-4
    public class Main {
    public static void main(String[] args) {
        String se = new String("Hello world");
        String sx = se;
        sx = "Hello aliens";
        System.out.println(se);
        System.out.println(sx);
    }
}

Here, se stores "Hello world" and sx=se which means sx and se must point to same memory location. If sx is changed to "Hello aliens" then se must also be changed. But the above code prints the following:

Hello world Hello aliens

Then how are Strings called Referential Data Types?

  • *"`sx=se` means `sx` and `se` must point to same memory"*! so, same logic, `sx="Hello aliens"` means that now `sx` points to same memory as `"Hello aliens"` (not `se` anymore) (( no need for `new String("Hello World")`; `"Hello World"` is already a `String` object {despite a different one} )) –  Mar 09 '21 at 17:58

4 Answers4

1

Strings in Java are immutable, which means you can't change them once they are created. You're not changing the originally referenced string; you are creating a new string.

This is how strings work throughout Java. If you look at the string manipulation methods for the String class in the Java documentation, you'll see that they all return a new string, rather than modifying the original one in-place.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
1

You created two completely separate pointers, that happen to be pointing to the same memory location:

String se = new String("Hello world");
String sx = se;

Those pointers are not linked together in any way. When you change one it doesn't change the other.

Here you are changing one of those pointers to point to a different memory location:

sx = "Hello aliens";
Mark B
  • 183,023
  • 24
  • 297
  • 295
1
    String se = new String("Hello world");

Now we have a variable se, which points to a string "Hello world". Simple enough.

    String sx = se;

Now we have a variable sx and a variable se, which both point to "Hello world". In this case, they point to the exact same string object, but generally speaking in Java we don't like to care about that; the JVM does just fine managing string objects without us having to think about it.

    sx = "Hello aliens";

Now we've changed what the variable sx points to. We haven't said anything at all about se. So sx points to "Hello aliens" and se to "Hello world", since we haven't changed anything on the se side.

If you're trying to understand reference semantics, strings are a bad example, since they're immutable in Java. Consider making a simple class like the following.

public class MyContainer {
  public int value;
  public MyContainer(int value) {
    this.value = value;
  }
}

Then make a few instances of MyContainer and do basically what you did with the strings with it. If two variables point to the same MyContainer and you change value on one, it'll be reflected in the other, but not if you change where the variable points.

MyContainer a = new MyContainer(0);
MyContainer b = a;
// a.value and b.value are both 0 here.
a.value = 99;
// a.value and b.value are both 99 here.
b = new MyContainer(100);
// a.value is still 99; we only changed b here.
Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
1
// "Hello world" literal is stored in StringPool
// se is a reference to the object String in heap that holds "Hello world"
String se = new String("Hello world");

// sx is the another reference to the same String object that se referes
String sx = se;

// "Hello aliens" literal is stored in StringPool
// sx is the same reference but now it refers to the "Hello aliens" literal in the StringPool
sx = "Hello aliens";

// print string by se reference, wich is "Hello World"
System.out.println(se);

// print string by sx reference, which is "Hello aliens"
System.out.println(sx)

I hope it helps you to understand of references.

Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35