Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Query in Strings -Pass by Reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Query in Strings -Pass by Reference" Watch "Query in Strings -Pass by Reference" New topic
Author

Query in Strings -Pass by Reference

Vinayatha Kumar
Greenhorn

Joined: Nov 10, 2008
Posts: 3
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?
Paul Campbell
Ranch Hand

Joined: Oct 06, 2007
Posts: 338
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 ]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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)...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Query in Strings -Pass by Reference
 
Similar Threads
Static/Non-Static
Passing String into a method causes the method to have it's own copy, correct?
Pass by value or reference?
question - methods - copy of a reference
a confused concept