About Me

My photo
Northglenn, Colorado, United States
I'm primarily a BI Developer on the Microsoft stack. I do sometimes touch upon other Microsoft stacks ( web development, application development, and sql server development).
Showing posts with label Integer. Show all posts
Showing posts with label Integer. Show all posts

Wednesday, November 07, 2007

Bug?? Reference variable arrays

A co-worker came to me with this problem:




    1         public static int[] MainArray = new int[2] { 0, 0 };


    2         static void Main(string[] args)


    3         {


    4             GetArray();


    5         }


    6         private static void GetArray()


    7         {


    8             MakeNewArray(MainArray);


    9         }


   10         private static void MakeNewArray(int[] p)


   11         {


   12             p[0] = 1;


   13             Console.WriteLine("p[0]:" + p[0].ToString());


   14             Console.WriteLine("MainArray[0]:" + MainArray[0].ToString());


   15             Console.ReadLine();


   16         }






Result:
p[0]:1
MainArray[0]:1

He wanted a copy version of the integer array, but instead it was acting like it was being reference. The response of the code seems wrong since you would expect it to make a seperate array and not reference the MainArray.

What seems to be happening is that since these are arrays, and of the type Reference. Then the arrays are placed on the heap, with the variable names placed on the stack (some what). So, when making a call to a seperate method, the new variable p is then placed on the stack, but pointing to MainArray's values on the heap. Giving a reference type behavior. The solution is to make a copy of the array.




    1         public static int[] MainArray = new int[2] { 0, 0 };


    2         static void Main(string[] args)


    3         {


    4             GetArray();


    5         }


    6         private static void GetArray()


    7         {


    8             MakeNewArray();


    9         }


   10         private static void MakeNewArray()


   11         {


   12             int[] p = new int[MainArray.Length];


   13             Array.Copy(MainArray, p, MainArray.Length);


   14             p[0] = 1;


   15             Console.WriteLine("p[0]:" + p[0].ToString());


   16             Console.WriteLine("MainArray[0]:" + MainArray[0].ToString());


   17             Console.ReadLine();


   18         }







Result:
p[0]:1
MainArray[0]:0

Tuesday, October 03, 2006

Java Question:

Well, I was just browsing the Code Project's website and found this interesting. Given the following Java code:

Posted by: Dominik Reichl

Integer i = 1;
Integer j = 1;
Integer n = 1000;
Integer m = 1000;

boolean b1 = (i == j);
boolean b2 = (n == m);


Now, what are b1 and b2?

Surprisingly b1 is true, b2 is false.

Why?
















Posted by: Dominik Reichl

Ok, so here's the solution why it works this way
First of all, we are using Integer wrapper classes instead of the primitive int type. When compiling, the integral values 1 and 1000 are implicitely converted to Integer objects (called auto-boxing, from int to Integer).

So, we got 4 different Integer instances, right? No!

The Java VM automatically packs "small" primitive values into identical objects (1 is small, 1000 isn't small). You can easily verify this by pasting the code above into a small testing project and fire up the debugger: i and j reference the same object, while n and m reference different objects. The == operator is comparing references, therefore b1 is true (same references) and b2 is false (different references).

Currently "small" means any integer between -128 and 127. But according to Sun this can change to [-32768, +32767] in the future. This packing trick has been introduced to solve performance problems when doing arithmetic with wrapper classes.