| Author |
If String is immutable, why is the following code working ?
|
Nagashri Kadur
Greenhorn
Joined: Dec 04, 2012
Posts: 4
|
|
public static void main(String[] args) {
String str1 = new String("This is a string");
str1 = "I thought this was immutable !! \n How \t did \t this \t happen ? ";
System.out.println("str1 = " + str1 );
}
The above mentioned code is compiling and is resulting in the following being displayed.
str1 = I thought this was immutable !!
How did this happen ?
If String is immutable how is this working ?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Nagashri Kadur wrote: public static void main(String[] args) {
String str1 = new String("This is a string");
str1 = "I thought this was immutable !! \n How \t did \t this \t happen ? ";
System.out.println("str1 = " + str1 );
}
The above mentioned code is compiling and is resulting in the following being displayed.
str1 = I thought this was immutable !!
How did this happen ?
If String is immutable how is this working ?
Can you tell us the difference between an object and a reference?
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Nagashri Kadur
Greenhorn
Joined: Dec 04, 2012
Posts: 4
|
|
Henry Wong wrote:
Nagashri Kadur wrote: public static void main(String[] args) {
String str1 = new String("This is a string");
str1 = "I thought this was immutable !! \n How \t did \t this \t happen ? ";
System.out.println("str1 = " + str1 );
}
The above mentioned code is compiling and is resulting in the following being displayed.
str1 = I thought this was immutable !!
How did this happen ?
If String is immutable how is this working ?
Can you tell us the difference between an object and a reference?
Henry
When you pass by reference, you pass the address. Am I correct ?
|
 |
Nagashri Kadur
Greenhorn
Joined: Dec 04, 2012
Posts: 4
|
|
|
When you pass by reference, the address gets passed. So, any changes made will be reflected. Is that right ?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Nagashri Kadur wrote:When you pass by reference, the address gets passed. So, any changes made will be reflected. Is that right ?
The question was ... can you tell us the difference between an object and a reference? which is very different from can you tell us the difference between "pass by reference" and "pass by value"? Both are very important to understand, but for this question, you need to understand the first distinction -- not the second.
To answer the question. Yes, string objects are immutable. And your example does not refute that. And you need to understand the difference between an object and reference first.
Henry
|
 |
Nagashri Kadur
Greenhorn
Joined: Dec 04, 2012
Posts: 4
|
|
Oh... OK. Guess there is way more to this than I thought. Thank you Henry ! Let me read up the difference... I really appreciate your guidance
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9950
|
|
look at this code:
and see if that helps you understand.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12928
|
|
One key thing to understand is: Variables are references. A variable is not the object itself - it's a reference to an object.
String objects are immutable. But if you have a variable that refers to a String object, you can still change the value of the variable itself (making it refer to another String object).
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
 |
|
|
subject: If String is immutable, why is the following code working ?
|
|
|