-1
String s1 = "abc";
String s2 = "abc";
String s3 = new String("abc");
String s4 = new String("abc"); 

if (s1 == s2) is giving true

while (s3 == s4) is giving false.

Can somebody give a detailed explanation onto what is String pool, heap, how many objects are created in each line and how many objects created in total.

Why s3==s4 is giving false?

A detailed explanation will be much appreciated.

Bahramdun Adil
  • 5,907
  • 7
  • 35
  • 68
CodeReaper
  • 377
  • 4
  • 18
  • 1
    Because you aren't comparing them the right way. http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Hanky Panky Mar 03 '16 at 08:35
  • Refer this question http://stackoverflow.com/questions/2486191/what-is-the-java-string-pool-and-how-is-s-different-from-new-strings – Ravi Ranjan Mar 03 '16 at 08:35
  • String should be compared using `equals()` method in Java. But yes, your question is about the `String pool`. – mzy Mar 03 '16 at 08:35
  • http://stackoverflow.com/questions/3052442/what-is-the-difference-between-text-and-new-stringtext – foundart Mar 03 '16 at 08:37
  • I don't think this question is about String Comparison. According to me it seems as if OP wants to know more about String Pools and how do they work. – user2004685 Mar 03 '16 at 08:39
  • Probably you got it right! Thanks for taking out time to atleast read the question. – CodeReaper Mar 03 '16 at 08:40
  • @CodeReaper I tried to explain the difference between the two comparisons that you did in the answer below. Hope it helps. – user2004685 Mar 03 '16 at 08:43
  • @CodeReaper I updated my quetion and I hope you can with this get a better look about how java and Strings are working... – ΦXocę 웃 Пepeúpa ツ Mar 03 '16 at 10:57

2 Answers2

0

When you do new String(...), it is evaluated at runtime and hence creates two different instances and you end up getting s3 == s4 as false. Same thing happens when you use StringBuilder and do something like sb.toString() which is again evaluated at runtime.

For Example,

StringBuilder sb = new StringBuilder();
String foo = sb.append("abc").toString();

String bar = new String("abc");

String foobar = "abc";

Here foo, bar and foobar are all different objects and hence foo == bar or foo == foobar will evaluate to false.

On the other hand s1 == s2 returns true because abc one declared, already exists in the String Pool and in this case both the objects points to the same reference in the pool.

user2004685
  • 9,548
  • 5
  • 37
  • 54
  • Why is this down-voted? What is wrong in this answer? – user2004685 Mar 03 '16 at 08:38
  • 1
    I haven't voted, but I can understand the downvotes. It makes no sense to repeat this answer multiple times. This newbie question gets asked several times a month, so it is much better to flag this question as a duplicate, instead of writing an answer. Well and one can think about downvoting OPs question for missing research. – Tom Mar 03 '16 at 08:53
  • 1
    @Tom Thanks for your response. I understood your point. – user2004685 Mar 03 '16 at 09:35
-1

String Class in java is defined in java.lang package and it is exactly that, a class and not a primitive like int or boolean.

Strings are developed to offer operations with many characters ans are commmonly used in almost all the Java applications

Some interesting facts about Java and Strings:

String in immutable and final in Java and in this case JVM uses String Pool to store all the String objects.

What are different ways to create String Object?

We can create String object using new operator like any normal java class or we can use double quotes (literal assignment) to create a String object.

There are too several constructors available in String class to get String from char array, byte array, StringBuffer and StringBuilderetc etc.

To your Question:

When we create a String using double quotes, JVM looks in the String pool to find if any other String is stored with same value. If found, it just returns the reference to that String object else it creates a new String object with given value and stores it in the String pool.

When we use new operator, JVM creates the String object but don’t store it into the String Pool. We can use intern() method to store the String object into String pool or return the reference if there is already a String with equal value present in the pool.

So when you do

String s1 = "abc";
String s2 = "abc";

those are checked in the StringPool and since s1 already exist there, s2 will take the same reference, hence, s1 ==s2 is true.

but when you do:

String s3 = new String("abc");
String s4 = new String("abc"); 

you are using the new operator, therefore the JVM is not checking if there is an string already in the heap, it will just allocate a new space for s4, so is s3==s4 ??? of course no.

Please take a look at the image below for a more illustrative example.

enter image description here

Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97