• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Passing a String reference to a method.

 
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Steve.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is an example of how variables are hidden/dominated in methods.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Richard,

That makes sense.

Roopesh.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic