• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

pass or copy by reference

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}

}
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tom chansky

Refer to this Thread.

Hope that helps.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I found some pretty shells, some sea glass and this lovely tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic