• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Query in Strings -Pass by Reference

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have started preps for SCJP and have joined JavaRanch recently. I have a question on passing Strings arguments to methods as references.

This is a test program that I coded myself to test the pass by reference for Strings -


Output that I got was :

Testing String........
In main-before func call: My String
In method convertString : New String
In main-after func call : My String

Below notes are from http://www.javacoffeebreak.com/articles/toptenerrors.html - Point no. 6 - Confusion over passing by value, and passing by reference

"When you pass a primitive data type, such as a char, int, float, or double, to a function then you are passing by value..........

When you pass a Java object, such as an array, a vector, or a string, to a function then you are passing by reference. Yes - a String is actually an object, not a primitive data type. So that means that if you pass an object to a function, you are passing a reference to it, not a duplicate. Any changes you make to the object's member variables will be permanent - which can be either good or bad, depending on whether this was what you intended.

On a side note, since String contains no methods to modify its contents, you might as well be passing by value."


Considering the above notes, the String argument to convertString(String) method should be pass by reference, and String variable re-assignment in //Line 1 should be reflected in the main method (considering that I have not used any String methods and have assigned a new value to the String). But after the convertString() is called, the output prints "My String" instead of "New String" in the main method in //Line 2, while I was expecting it to print "New String".

Can anyone please explain this?
 
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String objects are immutable, and String reference variables are not... In your example code... you simply created new strings (you created more than one actually) but didn't assign it to your reference variable.

This may be easier to show by example:



X = My String
Y = Your String
X = My String
Y = My String
X = My String My String
Y = My String

[ November 10, 2008: Message edited by: Paul Campbell ]
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well objects are not passed as reference. Actually if you take C++ as base, then they are passed as pointers. So modifying the object to which a reference points in a called method, doesn't change the reference in the calling method. Look at this example



So as you can see, if you change the object reference, then the original reference is not effected (s1 in this example) and if you change the state of the object, then the original reference is effected (s1 in this example)...
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When you pass a primitive data type, such as a char, int, float, or double, to a function then you are passing by value..........

When you pass a Java object, such as an array, a vector, or a string, to a function then you are passing by reference. Yes - a String is actually an object, not a primitive data type. So that means that if you pass an object to a function, you are passing a reference to it, not a duplicate. Any changes you make to the object's member variables will be permanent - which can be either good or bad, depending on whether this was what you intended.


These notes are incorrect. Forget about pass by reference - everything in Java is always passed by value.

Note that variables of non-primitive types in Java are references to objects - they are not objects themselves. You cannot really "pass an object", as the notes above say, because you don't deal with objects directly. Using sloppy language like this creates confusion about passing by value and passing by reference.

So, you don't "pass an object", but you pass a reference to an object, and that reference is passed by value.

Have a look at Pass-by-Value Please.
[ November 10, 2008: Message edited by: Jesper Young ]
 
It would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic