0

So I've read that if you write this:

String a="foo"; 
String b="foo"; 
System.out.println(a==b);

it will print "true", because the first implementation checks the memory pool looking for "foo", it cant find it so it creates a new object and puts foo in the memory pool, then every other string will be pointing to the same object.

and if you write:

String a="foo"; 
String b=new String("foo"); 
System.out.println(a==b);

it will print "false", because you force a creation of a new object for b so it wont take it from the pool.

my question is if you write this:

String a=new String("foo"); 
String b="foo"; 
System.out.println(a==b);

why does it still print "false" ? I mean "a" creates a new object and doesn't look in the memory pool, but b should look in the memory pool and find the object "a" created and point to it. what am I missing here? thank you.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Tallb
  • 21
  • 6
  • a is not in memoryPool, therefore a and b are pointing to dif references.... – ΦXocę 웃 Пepeúpa ツ Feb 10 '17 at 10:20
  • `String a=new String("foo");` creates 2 objects. First it creates `"foo"`, then it creates another one with `new String`. – marstran Feb 10 '17 at 10:20
  • It's easy, We have a String pool for String purpose, and Object pool for object, you can't compare a String in String pool to a String Object in Object pool using == directly – PSo Feb 10 '17 at 10:20
  • `String b=new String("foo");` That's a bad practice, you shouldnt use this. Use `equals` method for comparing strings. `==` tests tests for reference equality while `equals()` tests for value equality. – Zsolt Ébel Feb 10 '17 at 10:21
  • @ZsoltÉbel It's bad practice, but people should learn how it works anyway. – marstran Feb 10 '17 at 10:21
  • This is not a duplicate of "how do I compare strings in Java", voting to re-open. – Sergey Kalinichenko Feb 10 '17 at 10:44

5 Answers5

1

When you create a new Object it isn't in the pool.

b should look in the memory pool and find the object "a" created and point to it.

It's in the pool but it returns the "foo" object not the new String("foo") object so == is still false.

You can put an String into the pool with .intern() such as

String a = "food".substring(0, 3).intern();
String b = "foo";
assert a == b;
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

String a="foo";

-> Check String pool, if String pool does not have one, create a new String Object in String pool

String b="foo";

-> Check String pool, if String pool contains it, make a reference to that String Object.

String c=new String("foo");

It directly create a new String Object outside the String pool but in Object pool(Memory).

At object point of view, the 2 String Object are identical, so if you use .equals to compare those 2, it returns true. But if you use == to compare, it compare the reference, which is not pointing toward the same object, so it return false.

for more details: http://theopentutorials.com/tutorials/java/strings/string-literal-pool/

PSo
  • 958
  • 9
  • 25
1

You misunderstood the way the pool works: when you do

String a = "foo";

the String object "foo" ends up in the pool not because you create it first, but because it is an object based on a string literal (a sequence of characters inside double-quotes). Java creates this object for you in the constant pool.

When you write

String a = new String("foo");

the string object "foo" from the pool is copied into a new String object, which is not placed in the pool.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

Even though common pool is typically part of the heap, the objects created through new never goes into pool, where as the literals used while creating goes to pool. When you compare literals what you said will be true. Not against a literal and an Object.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
-1

String a does not go to string pool, String b does - so the references are different. That's why you get false.

Piotr Sołtysiak
  • 998
  • 5
  • 13