0

I am really confused in string pooling

how many objects will create in this situation

String a=new String("Hi"); //1
String b=new String("Hi"); //2
String c="hi"; //3

If in case of 2nd and 3rd string one object is being created, then why it is returning FALSE in case of (a==b)....

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Vikas
  • 11
  • 2
  • 2
    This has been asked so many times before ... For example here: http://stackoverflow.com/questions/10536670/how-many-string-objects-will-be-created-by-jvm-version-1-6. This one has even better answers: http://stackoverflow.com/questions/1881922/questions-about-javas-string-pool – Joachim Sauer Jun 10 '13 at 07:00

4 Answers4

4

why it is returning FALSE in case of (a==b)

Because you're using new String thus getting new object references for those Strings.

Note that changing the code to

String a = "Hi"; //1
String b = "Hi"; //2

will make System.out.println(a == b); printing true.

Further explanation: The String Pool

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • @JoachimSauer right, made a test to verify my statement (got confused with `Integer` pooling). Removed that part of the answer. – Luiggi Mendoza Jun 10 '13 at 07:14
2

In Java, for non-primitive types, the == operator compares references, not values.

If you create a bunch of equivalent String literals, like:

String sLit1 = "test";
String sLit2 = "test";

(sLit1 == sLit2) will be true, since Java doesn't allocate new memory for each new String literal, it just points them all to the same location in memory. However, when you create a String object:

String sObj = new String("test") 

Java always creates a new object, which occupies a new location in memory. So sLit1 == sObj will always be false.

Which means that == yields true if and only if the two arguments refer to the same object. To compare strings, use equals method, as in (sObj.equals(sLit1)).

Steve P.
  • 14,489
  • 8
  • 42
  • 72
  • Thanx , but one more thing, if i am using inturn() method, it is returning true in case of new keyword also – Vikas Jun 10 '13 at 07:10
  • Here you go, [java.lang.String.intern()](http://stackoverflow.com/questions/1091045/is-it-good-practice-to-use-java-lang-string-intern). – Steve P. Jun 10 '13 at 07:11
  • Also, since you're also a new user, you should check out the [tour](http://stackoverflow.com/about). – Steve P. Jun 10 '13 at 07:12
1

Its not one object ,You are creating Strings using new keyword,so jvm creates these Strings in Heap so both objects address is different.

And == check objects address thats why it is returning false.

and in case of String c="hi";

This is not creating using new so these literals creates in String pool.and there is interning process by jvm on these literals.

if there is one more String d="hi"; then c==d will return true.

ankita gahoi
  • 1,532
  • 2
  • 15
  • 28
0

Here,

String c = "Hi"; 

will create the string literal in String literal pool which is a separate memory provided for string literals in java. When you write

String c = "Hi";
String d = "Hi";

They will refer to the same literal in string constant pool. and == operator checks wheter reference variables refer to the same object

Hence, c == d will return true.

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56