Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Strings are immutable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Strings are immutable" Watch "Strings are immutable" New topic

Strings are immutable

sumaraghavi ragha
Ranch Hand

Joined: Nov 17, 2006
Posts: 118

How strings are immutable when compared with integers?

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

s= s.concat("de");

Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

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:


s -----> "abc"


[ August 03, 2008: Message edited by: Wirianto Djunaidi ]
subhasish nag
Ranch Hand

Joined: Apr 25, 2008
Posts: 101
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
it will only print abc .

Thanks,<br />Subhasish
sumaraghavi ragha
Ranch Hand

Joined: Nov 17, 2006
Posts: 118
Thaks for your reply!!

But can you please tell me why it happens to integers??
Jamie MacDonald

Joined: May 17, 2008
Posts: 20
[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

Joined: Oct 09, 2007
Posts: 206

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.

Enjoy, Chander
SCJP 5, Oracle Certified PL/SQL Developer
sumaraghavi ragha
Ranch Hand

Joined: Nov 17, 2006
Posts: 118
Then can i say all wrapper classes are immutable??
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3028
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

Joined: Nov 17, 2006
Posts: 118
Thans Mike and all others it's helped
I agree. Here's the link:
subject: Strings are immutable
It's not a secret anymore!