. - . Strings, ==, .
Java does not optimize, because there is nothing to optimize. If you use String literals, then identical ones are reduced to a single instance in the string pool, therefore
String foo = "foo";
String bar = "foo";
System.out.println(foo == bar);
However, with an explicit constructor
String foo = "foo";
String bar = new String("foo");
System.out.println(foo == bar);
However, since you should still use equals()objects for all associations, you will not see any difference when you run your code.
source
share