aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes array manipulation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "array manipulation" Watch "array manipulation" New topic
Author

array manipulation

Samith Nambiar
Ranch Hand

Joined: Mar 14, 2001
Posts: 147
hi ... can someone please tell me as to why the output in the following code is 1 and not 2
--------------------------------------------------------------
public class example {
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;
}
}
---------------------------------------------------------------
Thanx in advance
Samith
nachiket deshpande
Ranch Hand

Joined: Oct 06, 2000
Posts: 114
Hi Samith!
In your method
public static void change_i(i){
int j[]=2;
i=j;
}
when you say i=j;
you are changing the reference,(making reference i equals reference j)not the content of your original array which is i[0]
and so it still contains 1 and not 2.Hope this helps.
nachiket deshpande
Ranch Hand

Joined: Oct 06, 2000
Posts: 114
I am sorry method parameter is int i[] not i,typing mistake..
Balaguru Janarthanan
Ranch Hand

Joined: Nov 01, 2000
Posts: 37
hi Nachiket,
public class example {
public static void main(String args[]) {
int i[] = {1,2};
int j[] = {11,12};
i=j;
System.out.println(i[0]+""+i[1]);//prints 11,12
}
}
i hv tried this seems to be CONTENT of the arry has changed.
Tracy Qi
Ranch Hand

Joined: Apr 23, 2001
Posts: 37
Hi, here is the deal.
When u pass i as a parameter to a method. actually the complier make a copy of the reference. suppose it's "a"
so the method can be replaced by this
public static void change_i(int a[]) {
int j[] = {2};
a = j; //Line 1
}
in Line 1, when you do the assignment, actually u are assigning a to j, no i. i is still pointing to {1}.
put in another way, we have 3 reference of array now, i, j, a, You are manipulating a inside the method. changing reference of a won't affect i. But if the method change to this:
public static void change_i(int a[]) {
int j[] = {2};
a[0] = {2}; // Line 1
}
This will also affect i outside. here a do point to the same array as i. so if a changing value, i is also changing.
Hope this help.
Tracy
g krishnan
Greenhorn

Joined: Mar 30, 2001
Posts: 23
Hi guys
just imagine u are passing a obj reference here and look at the problem, when u change a obj reference inside a called method and return nothing happens, if u have to make changes then u have to access the instance variables and make changes like obj.i=5; etc then only the changes will reflect. To make changes reflect here in out case u will access i[0] inside the changed method and make the change.
u can run a similar code with objects and clarify your self.
hope i have helped
thanx
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array manipulation
 
Similar Threads
help!!
question about arrays
What is output and Why ?
doubt in arrays
Array - question