File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing a String reference to a method.

 
Marcelo Ortega
Ranch Hand
Posts: 528
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 528
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve.
 
Arulkumar Gopalan
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example of how variables are hidden/dominated in methods.
 
Roopesh Gulecha
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Richard,

That makes sense.

Roopesh.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic