Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about string immutable

 
Samuel Fitzpatrick
Greenhorn
Posts: 10
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49735
69
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 160
IntelliJ IDE Ubuntu VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ganesh Akondi
Greenhorn
Posts: 6
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Riaan and Campbell.It helps !!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49735
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic