Assign and create new String difference

java, string

Solution

The `String` objects that represent string literals in your Java source code are added to a shared `String` pool when the classes that defines them are loaded1. This ensures that all "copies" of a String literal are actually the same object ... even if the literal appears in multiple classes. That is why `s3 == s4` is `true`.

By contrast, when you `new` a String, a distinct new String object is created. That is why `s1 == s2` is `false`. (This is a fundamental property of `new`. It is guaranteed to create and return a new object ... if it completes normally.)

However, in either case, the strings will have the same characters, and that is why `equals` is returning `true`.

While it is important to understand what is going on, the real lesson is that the correct way to compare Java strings is to use `equals` and not `==`.

If you want to arrange that your String objects can be tested for equality using `==`, you can "intern" them using the `String.intern` method. However, you have to do this consistently ... and interning is an expensive process in various respects ... so it is generally not a good idea.

1 - Actually, it is a bit more complicated than that. They objects get added to the pool at some time between class loading and first use of the literals. The precise timing is unspecified and JVM implementation dependent. However it is guaranteed to happen just once, and before any application code sees the `String` object reference corresponding to the literal.

Problem

``` String s1 = new String("string"); String s2 = new String("string"); String s3 = "string"; String s4 = "string"; System.out.println(s1 == s2); //FALSE System.out.println(s2.equals(s1)); //TRUE System.out.println(s3 == s4); //TRUE System.out.println(s3.equals(s4)); //TRUE ``` What is the difference between creation of `s1` and `s3` ? Please let me know In String we are having only String object then why it treats this two differently. s1 and s2 are having different memory address while s3 and s4 has same memory address. why it works based on `new` operator.?

Original source