Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

passing the objects  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, All
Can you explain the output of the following code?

thanks
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
void go(){
String s="myString";
f(s);
System.out.println(s);//prints myString
String[] s2={"0","1"};
f(s2[0]);
System.out.println(s2[0]);//prints 0// copy of reference is null
f2(s2);
System.out.println(s2[0]); //prints null
}
/* copy of reference is null */
void f(String f){
f=null;
}
/* modifying the contents of the array.
Remember objects are passed by reference*/
void f2(String[] f){
f[0]=null;
}
 
Vitaly Oblap
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thks, Rahul.
I now that object are passed by reference, but for me it is not clearly why in case of f(String s) we have a copy of reference, the modification of this copy does not affects on original object.
In case f2(String[] s) we have a reference but NOT copy and the modifications of this object have the result for original object, created in go().
 
Rahul JG
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vitaly,
In case of method f(String s), you are setting the copy of the reference to null, you are in no way modifying the data which the reference points to.
In case of method f(String[] s), you are modifying the array which the reference points to.
Things would have been similar if method f(String[] s) also did something similar to s = null;
Consider there is a lock with 2 keys (one with me one with you).
In method f(String s), you loose your key (reference==null), nothing happens to the lock.
In method f(String[] s), you play around with the lock and then loose the key.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Java always passes by value. The reference is also passed as a value. So the new method will always have a copy of the reference. With this reference we can change the attributes of the object.
Please follow this link If Java uses the pass-by reference, why won't a swap function work? .
Hope this helps!
REgards,
Hari
 
Rahul JG
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to rephrase my statement:
Objects are passed by value but modified by reference.
 
Vitaly Oblap
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help.
Vitaly.
 
crispy bacon. crispy tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!