aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Passing a String reference  to a method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Passing a String reference  to a method." Watch "Passing a String reference  to a method." New topic
Author

Passing a String reference to a method.

Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

Please check this out:


Ok, so i know the rules about passing a primitive and an object refernce to a method. But str is a reference to a String object, so wouldn't line 2 modify the state of the object refered to by str at line one.
Or does get treated like a literal value?

Cheers,
Marzo.


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD, SCEA/OCMJEA

Live life to an interface, not an implementation!
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Strings are immutable; you can't change their value. Your code doesn't modify any strings - it creates a new one and returns it. Don't let the name of your argument confuse you. Your code is the same as...
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

Thanks Steve.
Arulkumar Gopalan
Ranch Hand

Joined: Oct 13, 2003
Posts: 104
Here is an example of how variables are hidden/dominated in methods.


Anbudan & Mahalo,<br />Arul<br /> <br />-Not a sun certified Java professional :-)
Roopesh Gulecha
Ranch Hand

Joined: Aug 09, 2005
Posts: 32
Hello,
Consider this variation.

public class Main{
static String str = "My String OBJECT";

public static void main(String[] args){
System.out.println(go()); //Line 1
System.out.println(str); // Line 3

}
static String go(){
str = "String Changed"; //Line 2 accessing global str
//str = str + " 2";
return str;
}

}

The output is
String Changed
String Changed

As I understand Strings being immutable means I cannot change what is stored in them. Like example from str = "Rich" to str = "RICH" is not possible. (Got this example from a book).
But this program shows otherwise changing the string. ( I am surely missing something)

Please help. Thanks.
Roopesh.
Richard Martin
Greenhorn

Joined: Jun 06, 2005
Posts: 2
This is how I understand it.

Strings are immutable, they cannot be changed. Their reference variables on the other hand are not immutable, they are weak and open to negotiation.

str = "String Changed"; //Line 2 accessing global str

This line did not actually change the String object referenced by str, it created a completly new String object gave it the value "String Changed" and then told str to reference the new String object. The old string object "My String OBJECT" still exists still exists on the heap but now nothing is referencing it so it becomes available for garbage collection.
Roopesh Gulecha
Ranch Hand

Joined: Aug 09, 2005
Posts: 32
Thanks Richard,

That makes sense.

Roopesh.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing a String reference to a method.
 
Similar Threads
Referenec passing
Query in Strings -Pass by Reference
Referenec passing vs. Valuse passing
string comparasion
static object initialization