This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 18113
    
  39

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: 18113
    
  39

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: 10912
    
  12

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: 13872
    
  10

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: If String is immutable, why is the following code working ?
 
Similar Threads
Question about Strings?
String concatination
Strings are immutrable?
comparing Strings
Strings Immutable ??