Reputation: 1133
I'm slightly confused and obviously missing something here:
I read that java.lang.String's "are constant; their values cannot be changed after they are created."
But if i write the following code:
String line;
line = "Test1";
System.out.println(line);
line = "Test2";
System.out.println(line);
The terminal outputs:
Test1
Test2
It appears I am able to set a value, and then set another value for the string later.
No difference if i try this way:
String line2 = "Test3";
System.out.println(line2);
line2 = "Test4";
System.out.println(line2);
I am still able to set the value after its been set initially.
Where have I gone wrong here?
Thanks.
Upvotes: 1
Views: 156
Reputation: 30875
The string are immutable, their values cannot be changed. That is true.
But in your code you are working with references.
String line; //allocate variable
line = "Test1"; //assign to variable value "Test1"
System.out.println(line);
line = "Test2"; //assign to variable value "Test2"
What you can not change are the strings itself not the variable reference.
The details JLS 15.26
This apply to objects types and primitive.
Upvotes: 4
Reputation: 1556
All answers above are all correct, String is immutable and you can't edit the object, the references in the other hand you can remove them or edit them.
Garbage collector will remove all objects that have no references since they can't be used anymore.
There are 2 memories in Java: Heap and Stack. The object live in the Heap while the variables live in the Stack.
When you create
String test1="Test1"
test1 is living in thes Stack and can be changed
However "Test1" is living is not in the Stack and you cannot edit it.
String test1, test2;
test1 = test2 = "MyString"
There if you change test1="Hello" test2 will not be affected and neither the string itself.
Person person1, person2;
person1 = person2 = new Person("Nickname")
person1.rename("My new name");
There in the other hand is going to edit the name of the Person (the object that is living in the Heap)
It didn't need to change the Stack reference and both person1 and person2 are going to be "affected" by this change.
Upvotes: 1
Reputation: 280168
See the comments
String line; // declares a variable of type String
line = "Test1"; // creates a new String object with value "Test1" and makes line reference it
System.out.println(line); // don't care
line = "Test2"; // creates a new String object with value "Test2" and makes line reference it
Those are two different objects.
Changing its value would be doing something like this
line = "Test1";
line[4] = "2";
System.out.println(line); // printing Test2
This is not possible in Java, since String
produces immutable instances.
There's a difference between assigning references and changing the value of an object.
Upvotes: 2
Reputation: 10147
There are two String values are created and allocated space in heap memory in JVM Test1 and Test2. What you are doing is just changing the reference address. Here is the bytecode of your code statements. Please just focus on ldc
opcode, as you can see that lcd
gets different String reference.
public class JavaConstant extends java.lang.Object {
public JavaConstant();
Code:
0: aload_0
1: invokespecial #8 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: ldc #16 // String Test1
2: astore_1
3: getstatic #18 // Field java/lang/System.out:Ljava/io/PrintStream;
6: aload_1
7: invokevirtual #24 // Method java/io/PrintStream.println:(Ljava/lang/String
;)V
10: ldc #30 // String Test2
12: astore_1
13: getstatic #18 // Field java/lang/System.out:Ljava/io/PrintStream;
16: aload_1
17: invokevirtual #24 // Method java/io/PrintStream.println:(Ljava/lang/String
;)V
20: return
}
Upvotes: 3
Reputation: 4713
Strings are immutable. You cannot change the value of String. You create a new String Object or refer it from the String pool.
String line; // line is at Location A
line = "Test1"; // Location A --> "Test1"
System.out.println(line); // Prints Test1
line = "Test2"; // Location A --> "Test2" --> Reference to Test1 is lost
System.out.println(line); // Prints Test2
Upvotes: 1