aspose file tools*
The moose likes Java in General and the fly likes Strings being immutable, then why? 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 » Java in General
Bookmark "Strings being immutable, then why?" Watch "Strings being immutable, then why?" New topic
Author

Strings being immutable, then why?

Jack Hunter
Greenhorn

Joined: May 22, 2010
Posts: 2


//Strings being immutable i and i2 are different objects.

Output :
false
i2Ajay
iAjay


Then i m using reflection to change the underlying char array used by string class for object i2.

Output:
TEay

i2TEay
iTEay
false


I don't understand why the i string object gets changed.

Can anyone please explain this behaviour?

thanks in advance.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. Joke names and "handles" aren't acceptable here. You can change your display name here. Thanks!

Now, as to your question: immutablity is not a promise of magic, nor any special internal property of an object. Immutability just means that an object's accessible interfaces provide no way to change its state. Reflection does an end run around that. In the case where security is important, like when the JVM is running downloaded code (an applet container, a RMI application) then the security manager can be switched on, and it can block attempts to use reflection in this way. But in a normal application, your code has full access to reflection -- but of course, this is a bad idea, because an awful lot of code assumes String won't change, and will break if that turns out to be false.


[Jess in Action][AskingGoodQuestions]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Also, please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read. You can edit your post by using the button.
Jack Hunter
Greenhorn

Joined: May 22, 2010
Posts: 2
Is it that both the String objects i and i2 uses the same char array in the background ? This might be due to the fact JVM caches string objects.

Could you please confirm?

Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 274

There's a small error on two lines of your code:


should be


Without the parenthesis it prints out the result of "before i==i2 ?:Ajay" == "Ajay", which is always false.

At first, I thought this was the reason why you were getting these strange results. But even with this fix, you get the same results. The two Strings are different instances, yet both char arrays are changed...very strange. It must have something to do with how Java interns Strings.


SCJP 6 || SCWCD 5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strings being immutable, then why?