File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Question about string immutable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about string immutable" Watch "Question about string immutable" New topic
Author

Question about string immutable

Samuel Fitzpatrick
Greenhorn

Joined: Jan 23, 2003
Posts: 10

The for loop is creating a string that already exists in the string pool ("1234").
In Head First Java appendix B #9, its seems to say it wont create another "1234" string because of "string immutable".
Why is another "1234" string getting created in the for loop?

Ganesh Akondi
Greenhorn

Joined: Jul 13, 2011
Posts: 6

The problem is not with the for loop or something else, i feel.
String str5 = ""+1234;
String str6 = ""+1234;

int i =1234;
String str7 = ""+i;

Obviously str5 and str6 are equal (== and equal)
when it comes to str7, we are appending integer i to null string via concatenation operator +
Here comes the magic..
When you switch on the debugger and see it traverse thru StringBuilder class and calls the methods append() and toString() which will return a new String object.
where as for ""+1234, it directly creating String via String class but if we perform operation like this (""+i) it is making use of StringBuilder class.
These are my observations, correct me if i am wrong.

-Ganesh
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
I think the real answer is that "" + i is not a compile-time constant. The JVM loads the String pool when the class is loaded. The compiler cannot verify that the value of i is still 1234 when the String object is created, so it cannot verify that its value will be "1234". The String is created at runtime, and its identity with the "1234" in the String pool cannot be tested. You can make it a compile-time constant by changing int i = 1234; to final int i = 1234;
It would be better to write
String.valueOf(i)
than
"" + i

You can put a String into the String pool at runtime. Try
String s = String.valueOf(i).intern();
and see whether that makes any difference.

By the way: always put spaces round binary operators. Not ""+1234 but "" + 1234
You do not have a null String there; it is wrong to call "" null. It is called the empty String.
Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 157

Ganesh Akondi wrote:
when it comes to str7, we are appending integer i to null string via concatenation operator +

Ganesh, an empty String ("") is not the same as null. Neither "".equals(null) or "" == null will be true.

An empty String has a length of zero, but you can call methods on it and use it just like any other String. A null String, on the other hand, will cause a NullPointerException to be thrown if you try to call any methods on it.


"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
Ganesh Akondi
Greenhorn

Joined: Jul 13, 2011
Posts: 6

Thanks a lot Riaan and Campbell.It helps !!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
You're welcome
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question about string immutable