This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!