public class Example1 { int i[] = {0}; public static void main(String args[]) { int i[] = {1};
change_i(i); System.out.println(i[0]); } public static void change_i(int i[]) { int j[] = {2}; i = j; } }
Please explain why the answer would be 1. Thanks.
amit taneja
Ranch Hand
Joined: Mar 14, 2003
Posts: 806
posted
0
the answer is simple chirta...
you are getting 1 because
int the change_i(int i[]) method you are declaring int[i] with the same name asyou declared it in main method ..but that is only the shadowing of that name and the one in change_i is just a reference to original int i[] of main(); so when you are assining j[] to the local reference of change_i[]..change is happening but only on local i[] not on the original one...
original one in main is still pointing to the object containing 1 integer primitive element...
hope it helps...
regards
Thanks and Regards,<br />Amit Taneja
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.