aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes passing by value (CHANGE OF OBJECT REFERENCE) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "passing by value (CHANGE OF OBJECT REFERENCE)" Watch "passing by value (CHANGE OF OBJECT REFERENCE)" New topic
Author

passing by value (CHANGE OF OBJECT REFERENCE)

sarim raza
Ranch Hand

Joined: Nov 02, 2000
Posts: 232
I would like to see an example where a String object, st
String st = new String("wow")
loses its object reference, when a method modifyIt(st), that takes a string object, is able to change the contents of the object by supplying something like "oops".
This should get returned and displayed in the main method, when a call to println is made. "oops"
Harpal Singh
Ranch Hand

Joined: Oct 10, 2000
Posts: 229
Dear sarim,
Strings are immutable....hence cannot be changed..but can be made to point to new object of there type...Hope the below code helps....
<pre>
class sarim
{
static String st = new String("wow");
public static void main(String[ ] args)
{

modifyIt(st);
System.out.println(" "+st);
}
static String modifyIt(String s)
{
s=new String("oops");
st=s;
return st;
}
}
</pre>
Thanks,
Harpal
 
 
subject: passing by value (CHANGE OF OBJECT REFERENCE)
 
Threads others viewed
passing strings to methods
String Literals
String Concat
rst.updateString(..) not working
Thread ...
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture