-3
public static void main(String[] args){
    String str1 = "Hello";
    System.out.println(str1);
    tell(str1);
    System.out.println(str1);
}

public static void tell(String str2){
    str2 = "Hi";
}

output: Hello
        Hello

Is this example suitable for string immutable? I think reference str2 might changed str1 String value caused both of them reference to the same address. If the String class is mutable, the output might be Hello Hi, and because of the immutable characteristic, the final result is Hello Hello.

Is the reason right for this example?

http://www.explain-java.com/is-string-mutable-in-java-why-wrapper-classes-immutable-in-java/

This web page has the same example as me. Am I really wrong?

If I am right, plz vote me up to let more people know the truth of String immutable.

Leo Hsieh
  • 351
  • 4
  • 12
  • You would need to do `str1 = tell(str1);` and change the return from `void` to `String`, as java is pass-by-value-of-reference, so when you reassign in the `tell` method, that reassignment does not escape the closure of the method itself. – Rogue Dec 25 '14 at 03:07

4 Answers4

2

This is not an example of immutability but rather that object references are passed by value and not by reference. So assigning to an argument doesn't change the value of a variable that was passed to it.

Although tell() clearly assigns "Hi" to str2 which is its argument, it doesn't change the value of str1 within main().

Dan D.
  • 73,243
  • 15
  • 104
  • 123
2

A String being immutable means you can't alter the string; it says nothing about replacing the contents of a String variable with a new string.

Your output has nothing to do with the mutability (or not) of Strings, as you never try to change a String. You do try to change an argument from within a function, but because Java passes everything by value, while str2 will in fact be "Hi" inside of tell, that assignment will have no effect on str1 (as you have seen).

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
1

Java passes everything by value not by reference. In fact a copy of the reference is passed. Because Strings are immutable, your assignment creates a new String object that the copy of the reference now points to. The original reference still points to "Hello". Check this article to better understand why and which are the benefits

Kostas Kryptos
  • 4,081
  • 2
  • 23
  • 24
1

Examples of String immutable or not?

Case1: In this case string "java2s" is immutable.

class Main { 
    public static void main(String args[]) { 
        String s = "java2s"; 
        s.replace('a', 'Z').trim().concat("Aa"); 
        s.substring(0, 2); 
        System.out.println(s); 
    } 
}

Output: java2s

Case2: In this case string "java2s" is immutable but the new string assign to 's' reference variable.

public class Main {
  public static void main(String args[]) {
    String s = "java2s".replace('a', 'Z').trim().concat("Aa");
    s.substring(0, 2);
    System.out.println(s);
  }
}

Output: jZvZ2sAa

SilverNak
  • 3,283
  • 4
  • 28
  • 44
Atishay
  • 41
  • 9