+ 4

The last post relative to reference and value

I hope that is true ...value type hold the value of the variable in the stack .... reference type hold just the pointer of the variable in the stack so when I say int x=5,y=x; both of them are stored on stack like that x=5 y=5; and when I change y value x will not be changed but when I say/write int[]x={1,2},y=x; the pointer x and y will be stored in stack and the array will be stored in heap x and y are pointing to the same address so when I change for example y[0]=0; x[0] will be changed also because x and y are pointing to the same address The point is here ...when I say String x="hellow",y=x; x and y will point to the same address on heap but when I say y="Hi"; x will not be changed because Strings are immutable the reference define is : the variable pointer is stored on the stack and the variable value is stored on heap . was that true ?😊 //in order to not misunderstand I am not trying //to advertise any thing I just want to learn truly thanks for help 😊💓

11th Sep 2018, 10:47 AM
ABADA S
ABADA S - avatar
3 ответов
- 1
True, if you only talk about local variables. If you have a field in an object with value type, it will be stored in the heap as part of the object. Local variables and method parameters are - if value type - being stored on the stack, right. Two variables having references to the same array behaves just like you described. The reference x and y hold points to the same object, hence x[0] and y[0] are exactly the same thing. The "... point is here ..." part is also true. String x and y have reference to the same String object - by high chance in heap, but as the exact location and workaround is not clear to me. (See: https://stackoverflow.com/questions/9991701/is-a-string-literal-stored-on-the-stack-is-a-new-string-stored-on-the-stack There is a StringPool in Java, which complicates the whereabout of a String.) The easiest in my opinion is to think about this as the followings: If you use the = (assignment) operator directly, then it will break all connections with everything else - at least this is true for Java, not necessarily for others like C# (where there is the ref keyword). So, if you code: String a = "First"; String b = a; b = "Second"; Then, as you use = (assignment) directly on variable 'b' it won't influence anything else except the variable 'b'. Everything else will remain the same, including variable 'a'. To make a change in 'b' which also notable in 'a', then you use 'b.something =' (you use assignment NOT directly on 'b' but on 'b.something'). The array indexing can also be categorized as such indirect operation: int[] x = new int[5]; int[] y = x; //x and y now references the same array object in the heap //We use assignment not on x, but on x[0]: x[0] = 42; //y[0] is also 42, same thing //We use assignment directly on x: x = new int[2]; //"connection" with 'y' is broken, they point elsewhere
12th Sep 2018, 2:09 PM
Magyar Dávid
Magyar Dávid - avatar
+ 3
I understood what you said and I think I accept with you in my aspect thanks for you
12th Sep 2018, 5:54 PM
ABADA S
ABADA S - avatar
+ 1
In the C language or C++ you would see the mechanics of pointers more clearly. The following is still Java code, I just present the ideology: int[] x = new int[5]; //new int[5] allocates the memory for a 5 length int array (in heap) //then in Java returns an int[] reference to it. //stores (on stack) the int[] reference in 'x' pointing to the allocated memory int[] y = x; //copies/clones (!) the reference as value type from x and stores it in y. //imagine it like both are pointers and store the same //memory address (number) for the int[] array in heap y[0] = 10; //writes the data of #1 int element in the array on the stored address //since x points to the same address, it sees the same value on [0] y = new int[2]; //'y' is assigned a new reference of a new int[] object //which has nothing to do with the former value //and so 'y' now has nothing to do with 'x' neither changed the first array
12th Sep 2018, 2:23 PM
Magyar Dávid
Magyar Dávid - avatar