aspose file tools*
The moose likes Beginning Java and the fly likes pass or copy by reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "pass or copy by reference" Watch "pass or copy by reference" New topic
Author

pass or copy by reference

tom chansky
Greenhorn

Joined: Nov 01, 2004
Posts: 28
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

Joined: Feb 18, 2005
Posts: 58
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

Joined: Feb 05, 2003
Posts: 4727

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.


A good workman is known by his tools.
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Tom chansky

Refer to this Thread.

Hope that helps.


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Kenneth Brooks
Greenhorn

Joined: Feb 21, 2005
Posts: 9
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pass or copy by reference