aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array - question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Array - question" Watch "Array - question" New topic
Author

Array - question

Chitra AP
Ranch Hand

Joined: May 25, 2005
Posts: 42
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
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.
 
subject: Array - question
 
Similar Threads
question about arrays
help!!
doubt in arrays
What is output and Why ?
Arrays