Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

If String is immutable, why is the following code working ?

 
Nagashri Kadur
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nagashri Kadur
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you pass by reference, the address gets passed. So, any changes made will be reflected. Is that right ?
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
look at this code:


and see if that helps you understand.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15484
43
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic