Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pass or copy by reference

 
tom chansky
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) String class is an object, correct? So, the following statement "test.testString(s)" is passing/copying a reference of String s to testString(), correct?

2) So, I aslo assume in "String ss = s" that ss is now pointing to the same memory location as s does, correct? If so, then I also assume that the content of String s is now modified and became "Hello World", right? Well, after I ran the program, the result tells me different things:

from testString() Hello World
from main() hello


************************************************************************

public class Test {

public static void main(String[] args){

Test test = new Test();

String s = "hello";
test.testString(s);
System.out.println("from main() " + s);

}

public void testString(String s){

String ss = s;
ss = "Hello World";

System.out.println("from testString() " + ss);
}

}
 
Igor Stojanovic
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tom,

In your main method you make String object "hello" and you assign "s" reference to "hello" object in memory; and now when you send "s" reference to your testString() method, reference is sent by value,so method makes her own copy of "s" and puts that copy value in "ss"; at this point both "s" and "ss" point to same object in memory; after that testString() method reassign "ss" reference to new String object "helloWorld",but "s" didn't change initial value so it still points to object "hello". "s" and "ss" can both have their values pointing to same object in memory or each could point to different object,changing "ss"'s value doesn't effect value stored in "s"'s reference because testString() work's with copy of "s" from main().


kind regards
Igor
[ February 24, 2005: Message edited by: Igor Stojanovic ]
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, your testString method can't change the String that s references. String is an immutable object, which means that the object can not change after it has been created.
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom chansky

Refer to this Thread.

Hope that helps.
 
Kenneth Brooks
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
might be more helpful to think of the ss = "Hello World";
as ss = new String("Hello World"); which is exactly what happens.
even tho you think you are replacing the value of ss (which is what
ss = "Hello World"; looks like it is doing) you are really just replacing the value of the object reference. Meaning, that you have created a new object and are pointing the reference (ss) to that new object.

-k
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kenneth Brooks:
might be more helpful to think of the ss = "Hello World";
as ss = new String("Hello World"); which is exactly what happens.
even tho you think you are replacing the value of ss (which is what
ss = "Hello World"; looks like it is doing) you are really just replacing the value of the object reference. Meaning, that you have created a new object and are pointing the reference (ss) to that new object.

-k


I'm sorry, Ken, but this is completely wrong.

String ss = s;

makes the reference ss into a copy of the reference s. No new objects are created. Afterwards, ss and s refer to the exact same object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic