This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings are immutable

 
sumaraghavi ragha
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

How strings are immutable when compared with integers?

But we are changing the string values like
String s = "abc"

s= s.concat("de");

Thnaks
 
Wirianto Djunaidi
Ranch Hand
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because you are not changing the string that is referenced by 's', but you are replacing the 's' with the reference of a new object that is resulted by s.concat('de').

Does that make sense?

In ASCII art:

Orig:

s -----> "abc"

Then:

[ August 03, 2008: Message edited by: Wirianto Djunaidi ]
 
subhasish nag
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here value of String object (abc)is not changing . Only the reference s indicating seperate String object.We just lost the reference to object abc which was earlier indicating by s.
For this reason if you try with just
s.concat("de");
System.out.println(s);
it will only print abc .
 
sumaraghavi ragha
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thaks for your reply!!

But can you please tell me why it happens to integers??
 
Jamie MacDonald
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[I assume you are referring to int (the primitive type) and not Integer (the wrapper object. In that case...]

An int variable, holds the actual int value, not a reference to an object on the heap. In the examples above, the int is equivalent to the object reference, not the object itself. So just as the bit pattern representing the object reference changed when you assigned a reference to a new string, the bit pattern representing the int value changes when you assign a different int value to the variable.
 
chander shivdasani
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there are two types of integers, one is the primitive int and the other is the Wrapper class called Integer.

The Wrapper class behaves just like the other class in Java and it is stored in heap. The explanation mentioned above also stands true for wrapper class. When you try to change a value, internally a new object is created.
 
sumaraghavi ragha
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then can i say all wrapper classes are immutable??
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really. The term "wrapper" isn't formally defined anywhere I know of, and it's entirely possible that someone might use the term in a sense that includes mutable objects. For example, a java.util.Date can be thought of as a wrapper around a long. But for some unknown reason (most likely, someone just screwed up) a Date is mutable.

On the other hand, as far as I can see, all the wrapper classes in the java.lang package are immutable. Those are the ones involved in autoboxing and unboxing. If that's what you meant, then yes. But it's not really true in general.
 
sumaraghavi ragha
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thans Mike and all others it's helped
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic