3

if string objects are interned then why change in one does not affect other

public class EqualExample {
public static void main(String[] args) {

    String str = new String("Hello");
    String str1 = new String("Hello");

    System.out.println(str == str1);
    System.out.println(str1.equals(str));

}

}

Output of above programe would be

false true

    public class EqualExample {
    public static void main(String[] args) {

    String str = "Hello";
    String str1 = "Hello";

    System.out.println(str == str1);
    System.out.println(str1.equals(str));

}

}

Output of above code is

true true

this is because in string pool Heloo alredy exists so it intern the string and refer to same object then why if i change str1 to "heloo java" then why str still have value "heloo". because they refer to same objects so the value of str must be change public class EqualExample { public static void main(String[] args) {

    String str = "Hello";
    String str1 = "Hello";

    System.out.println(str == str1);
    System.out.println(str1.equals(str));

    str1="Heloo java";
    System.out.println(str+str1);
    System.out.println(str == str1);
    System.out.println(str1.equals(str));

}

}

output true true HelooHeloo java false false

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • 1
    `String` objects are immutable. You cannot change the content of a `String` object. – Jesper May 22 '15 at 19:18
  • Answer: Because strings are immutable, if you change value of the string it's the different string. – Yoda May 22 '15 at 19:19

5 Answers5

11

str1 is not a String. It is a reference to a String object. By doing

str1 = "Heloo java";

you're not modifying the String object, you're simply making the reference point to another, different String object.

Before:

str --------> "Hello"
                ^
str1 -----------|

After:

str --------> "Hello"

str1 -------> "Heloo Java"

Changing the object would consist in doing something like

str1.setCharacters("Heloo Java");

but such a method doesn't exist, because Strings are immutable. Their characters thus can't be modified (except by using dirty reflection tricks).

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
6

You have two pointers pointing to the same address in memory. Changing one value means redirecting one pointer while the other one remains unchanged.

blueygh2
  • 1,538
  • 10
  • 15
1

You can't change an existing String. String is immutable. When you say 'changing str1 to "heloo java"' you are in fact assigning a reference to a different immutable object.

oᴉɹǝɥɔ
  • 1,796
  • 1
  • 18
  • 31
1

According to Java Docs, Strings are Immutable. That is: they don't change, or, once created: they never change (the state of the Object, not the reference variable.)

However, you can change the reference variable, creating a new String Object , in an existing reference variable. If there is no reference variable pointing to the "old" String Object, the "old" String Object is considered inaccessible by your program, and the Garbage Collector may clean it.

in practice, with comment explaining in the code:

    public static void main(String[] args) {
    
    String s1 = "Test 1";
    
    String s2 = s1;
    
    System.out.println("s1: "+s1+ " and s2: "+s2);
    
    s2 = "String 2";
    
    System.out.println("s1: "+s1+ " and s2: "+s2);
    
    //Garbage Collector will not clean the "String 1" because we still have the s1 Reference Variable pointing. 

}

OUTPUT:

s1: Test 1 and s2: Test 1

s1: Test 1 and s2: String 2

As you mentioned, to offer better performance, JVM don't create a new String object in the Heap if the new String created is "meaningfully equal" (pass in the String.equals method) to a existing one in the Heap. But you can force this creation by new String("test 1") in the reference variable.

This Immutable state of String is to guarantee the good work of your program. Imagine how dangerous could be if you have two references of String and one of then changes: the other will be changed too and Strange Things could happen.

Java Classes are Immutable too; like the Wrapper Classes: java.lang.Integer, java.lang.Double, ....

I have illustrated the Garbage Collector to you deeply understand the Reference Variable relation with the Object in the Heap.

Community
  • 1
  • 1
G Bisconcini
  • 764
  • 2
  • 6
  • 25
1
String str = "Hello"

Means

if "Hello" exists in the pool,
      then return "Hello" instance reference from pool
else
    create an object "Hello". Keep the object in the pool, then return reference

But

String str = new String("Hello");

means

Always create "Hello" object, irrespective of whether it exists in the pool or not.

K139
  • 3,654
  • 13
  • 17