aspose file tools*
The moose likes Beginning Java and the fly likes If String is immutable, why is the following code working ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "If String is immutable, why is the following code working ? " Watch "If String is immutable, why is the following code working ? " New topic
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: 18896
    
  40

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: 18896
    
  40

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: 11406
    
  16

look at this code:


and see if that helps you understand.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

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 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: If String is immutable, why is the following code working ?