• Post Reply Bookmark Topic Watch Topic
  • New Topic

String Comparision  RSS feed

 
Prakash Pasumarthy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String s="abc";
String s1=new String("a")+new String("b")+new String("c");
String s2="a"+"b"+"c";

System.out.println(s==s1) ; returns false.
System.out.println(s==s2) ; returns true.

(s==s2), s, s2 were saved in stack,String pool concept . thats why returns true.
String s1=new String("a")+new String("b")+new String("c");
Can any one tell me , s1 will be saved in heap or stack ? what happens when new String("a")+new String("b")+new String("c");
is executed in memory i.e stack & heap.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For Java compiler "abc" and "a"+"b"+"c" both are same hence true
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
objects are always created in heap. nowadays people says there is also possibility to create object in stack for a object which is created inside a method . Escape_analysis
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and Welcome to JavaRanch Prakash
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This isn't a Stack versus Heap issue: all of the Strings are Objects and therefore (barring escape analysis) are stored in the Heap. This is all about when Strings can be retrieved from the String Pool.

String s = "abc";

When this code gets loaded, "abc" as a String literal will be created and stored in the String literal pool. The variable s will be assigned a reference to the String stored in the pool.

Generally speaking, 'calculated' Strings are created when the code executes and the value does not come from the String literal pool. There is a special case for when the calculated String is a series of concatenated String literals. These will be converted (at compile time I believe) to a single String value definition, and as such the String in the String literal pool can be used. So:

String s2 = "a" + "b" + "c";

looks just like

String s2 = "abc";

And s2 will get a reference to the "abc" String in the literal pool.

The final line of code is not the same. It creates a calculated String from a series of String Objects that are not String literals. new String("a") creates a new String Object when the code is executed that is not a String literal*. Since the 3 parts of the s1 definition are not literals, they can't be concatenated at compile time and the results are not treated like a literal, and therefor the result is not stored in nor retreaved from the Sting literal pool.

String s1 = new String("a") + new String("b") + new String("c");

creates a series of new String Objects when the code is run, those Objects are concatenated and a new String Object is created. s1 is assigned a reference to this new String Object.

When you compare using the == operator, you are comparing identity, and since s and s2 both point to the same Object in the literal pool the comparison is true. Since s1 points to a different Object the comparison is false.

*Actually the line of code creates 2 String Objects for each part of the concatenation. When the code loads the three String literals "a" "b" and "c" are created and added to the String literal pool. Then when the code is run those String literal Objects are used to create a new String Object which does not get into the pool.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This JavaRanch Journal Article contains all sorts of useful information about Strings.
 
Prakash Pasumarthy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve & Seetharaman Venkatasamy
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!