Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

cant understand Y ?  RSS feed

 
sudhan Sadanand
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i dont seem to understand this output and id be glad if someone can reason it out.

public class example {
int[] i = {1};
public static void main(String[] args) {
int[] i = {2};
change_i(i);
System.out.println("In main method : "+i[0]);
}
public static void change_i(int[] i) {
int[] j = {4};
i = j; //line 10

System.out.println("In change method : "+i[0]);
}

}

The output is:
In change method: 4
In main method: 2

when line 10 is changed as

i[0] = j[0];

then the output is:
In change method: 4
In main method: 4

Can someone explain why this happens?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main point to understand here is that all parameter passing in Java is by value.

When the type of the formal parameter is an object reference, then a copy of that actual parameter is sent to the method.

As long as that formal parameter still refers to the original object, you can make changes to it, assuming it is mutable.

But if you point that formal parameter to another object, then you can't affect the original object with it.

In the first case, i=j changes where the formal parameter i points.

In the second case, i[0]=j[0], i still refers to the original array and so you can change its elements using the reference i.
 
Arnav Velimala
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe it is related to this
post
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Y?Y!

Please UseRealWords
[ June 12, 2007: Message edited by: David O'Meara ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sudhan,

Following what Keith explained concerning "passing by value", you might also like to read the "Pass-by-Value Please" story in the JavaRanch Campfire Stories. This story certainly helped me to understand Java references (and objects) better.

If you're still not confident in understanding how the example application works, then it may help others to help you, if you were to explain some more details about what you expect the program to do, and how you currently understand what the program actually does.
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In simple speak, when main() called change_i(), change_i() got it's own copy of the reference to i[] in the main routine. It still pointed to the same array object, but it was a separate distinct copy.

When line 10 says: i = j you are actually changing the reference that the i variable points to and making it point to the j array object. In that case any modifications of elements in i[] AFTER LINE TEN will actually occur to the j[] which is local to change_i().

Now, when line ten says i[0] = j[0] what you are now telling the compiler to do is to use whatever happens to be in reference variable i AS a reference and set it's 0th element to the same value as j[0]. Since i was passed in as a reference to main's i[], that line will change the value of main's i[0] in place.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!