Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings being immutable, then why?

 
Jack Hunter
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


//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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic