Question: 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?

Answer: 
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.
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()](http://download.oracle.com/javase/6/docs/api/java/lang/String.html#intern%28%29). 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](http://java.sun.com/docs/books/jls/html/)
> 
> 
>