1

I recently learned that in Java: == compares the object references, not the content, which is why:

String str1 = "hell";
String str2 = "o";
String str3 = str1 + str2;
String str4 = "hello";

str3 == str4; // False

So far so good. However when I do the following:

String str5 = "hello";
str5 == str4; // True

Does this mean that str5 and str4 reference the same memory object? How does this work?

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
matthijs
  • 489
  • 1
  • 8
  • 20
  • possible duplicate of [Strings are objects in Java, so why don't we use 'new' to create them?](http://stackoverflow.com/questions/2009228/strings-are-objects-in-java-so-why-dont-we-use-new-to-create-them) – Vincent van der Weele Jun 30 '14 at 08:19
  • 1
    It's worth noting that although in practice all well know JVMs use string pooling they are not obliged to. Don't rely on string pooling happening – Richard Tingle Jun 30 '14 at 10:02

7 Answers7

6

The String str5 = "hello"; creates a pooled String value hello, which is why str5 == str4 returns true.

On the other hand, str1 + str2 works like this:

More info:

Community
  • 1
  • 1
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
  • Is this 'pool creation' specific for strings? Because if I have a ` `class 'Car' and: car1 = Car('Toyota'); car2 = Car('Toyota'); then car1 == car2 // returns false` – matthijs Jun 30 '14 at 08:39
  • 1
    Yes, it's specific for Strings (and the `Integer` objects between -128 and 127) :) – Konstantin Yovkov Jun 30 '14 at 08:45
3

Yes. str5 and str4 refer the same memory object. As Strings are immutable when you change the value of some string its produce an different object. If two String objects have the same value then second one is not created, JVM just give the reference of the first object.

When the value of String changed different object created for some security and other usefull purpose read these link:

Immutability of Strings

wiki Immutable object

When some string are created like

String str1="hello";

JVM creates an immutable object when again you try to create some string with same value

String str2="hello" 

JVM use the same procedure to create an object as see's that this object is already created then its return the object of the str1 to reduce duplicate object creation. This will be useful string pool in the jvm

Community
  • 1
  • 1
Saif
  • 6,804
  • 8
  • 40
  • 61
1

Yes, when you create and assign a String value eg String s1="hello"; , it gets added in the String pool. Now if you assign the same String value to another reference like this:-

String s2="hello";

The variable s2 will point to the same String object hello , present in String pool.

However you can force and create a new String object for the same values like this:-

String s3= new String("hello");

This will add and create new object for hello even though it is already present in the String pool.

Hence it can be summarised as:-

s1==s2; //return true
s1==s3; //return false
s2==s3; //returns false
Mustafa sabir
  • 4,130
  • 1
  • 19
  • 28
0

Strings are pooled by the JVM once they are created, that's why those variables refer to the same instance in the String pool.

And the idea behind having a String pool is to avoid unnecessary object creation.

Juvanis
  • 25,802
  • 5
  • 69
  • 87
0

Strings are immutable objects. This is why str1 and str2 combined are not equal to str3.

PKlumpp
  • 4,913
  • 8
  • 36
  • 64
0

Ya,its gives true for str5 ==str4 becoz it uses"string pool area" to store."this time also it compares object references" but these strings has same object reference as string pool area has one object id. In first case strings are not created in string pool area thats why it gives false.

0

A private pool of string literals is maintained by String class. All literal strings and string-valued constant expressions are added to this pool during program start up. So all string literals with same value will point to same object.

When a new string object is created(e.g. by concatenating two string objects) it does not belong to the String pool, so comparing it with another string literal will return false. Any string object can be added to String pool by invoking intern() method on that string object, now the object will point to a string literal from the pool which have same value as this object. Now any string comparison of this object with same string literal will yield true.

Sanjeev Kumar
  • 680
  • 8
  • 16