-1

On the one hand;

String first = "thing";

String second = "thing";

if(first == second)

System.out.print( "Same things" );    //this is printed

On the other hand;

String first = "thing";

String second = new String("thing");

if(first == second)
{
 System.out.print("Same things");
}
else
{
 System.out.print("Different things"); //This is printed
}

I know that " == " operator is using for the comparison of references of two objects, but in the first example, I compared the values of objects directly. I know this kind of comparison is inexact. But why do I get the message in first example? Is it an indicator that references are the same, or is it occured by a coincidence?

El3ctr0n1c4
  • 400
  • 2
  • 7
  • 18
  • Why is this confusing? In the first example, `first` and `second` both point to `"thing"`, which is obviously the same string. In the second example, `second` points to a new `String` object that contains the text `"thing"`. When using `==`, the contents of the `String` are not considered, so the fact that both `String`s contain the text `"thing"` is irrelevant. – Nate W. Nov 07 '11 at 20:50
  • possible duplicate of [Strings of Same Value in Java?](http://stackoverflow.com/questions/6558285/strings-of-same-value-in-java) – Reverend Gonzo Nov 07 '11 at 20:52
  • There are same strings but in spite of creating different objects, it is returned true. But references should have been different. This is why I am confusing – El3ctr0n1c4 Nov 07 '11 at 20:57

6 Answers6

3

in first example String pool was used - if you don't use new keyword - every new instance of String lands on that pool, and if String with same value is created - no new object is made - that one on pool is being used.

dantuch
  • 9,123
  • 6
  • 45
  • 68
  • 1
    small addition: and if you use new keyword - normal object is created and kept where other objects - on the heap. – mmatloka Nov 07 '11 at 21:01
0

I think that's how Java optimizes by caching strings created using this method:

String second = "thing";

And therefore makes them "equal".

When doing this:

String second = new String("thing");

You explicitly allocate a new string with those contents, which makes your if condition false.

Blender
  • 289,723
  • 53
  • 439
  • 496
0
String first = "thing";

String second = new String("thing");

if(first.equals(second))
{
 System.out.print("Same things");
}
else
{
 System.out.print("Different things"); //This is printed
}

This should be work.

Smeagol86
  • 15
  • 1
  • 10
0

In the first example, Java is assigning exactly the same object to first and second.

In the second example, Java is creating a new Object 'second' (which internally points to the first). So what you have are two string objects first and second, but second internally contains a reference to first.

See the documentation for the String#intern(). From that:

Returns a canonical representation for the string object. A pool of strings, initially empty, is maintained privately by the class String.

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

All literal strings and string-valued constant expressions are interned. String literals are defined in §3.10.5 of the Java Language Specification

Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
0

In the first, first and second have same reference.

But in the second, they have different references.

With == you are actually comparing the values in the variables first and second, which are the references to the string objects. You are not comparing the objects themselves. Which would be done using the equals method.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
  • Actually I don't care the equals() method, It has to be used. Thanks for your interest. – El3ctr0n1c4 Nov 07 '11 at 21:00
  • @El3ctr0n1c4: "but in the first example, I compared the values of objects directly", sounds like your thinking that you are comparing the content of the objects there. So, I was just trying to make sure that you know by using `==` you are comparing the contents of the variables `first` and `second` instead of the contents of the objects they are referring to. – Bhesh Gurung Nov 07 '11 at 21:23
  • You're right, I was mistaken at that place. I try to improve my English :) – El3ctr0n1c4 Nov 07 '11 at 21:30
0

string literals are interned you would also get the message with

String first = "thing";

String second = new String("thing");

if(first.intern() == second.intern())
{
 System.out.print("Same things"); //This is printed
}
else
{
 System.out.print("Different things");
}
ratchet freak
  • 47,288
  • 5
  • 68
  • 106