But if you declare like this
String s3= "JAVA";
String s4= new String("JAVA");
In this case s1 & s2 both are denoting different objects. because new create a brand new object in String pool.if you want to create a bracnd new object in String pool you have to use String constructor. For E.g
s1==s2 will return true While
s3==s4 will return false.
the reason is s1 & s2 both are denoting same object while s3 & s4 are denoting different objects
Jesper Young wrote:They don't remove it from the API because that might break existing programs in which this constructor is used. Sun is always very careful with backward compatibility when a new version of Java comes out. Sometimes methods or classes are deprecated, but they are never actually removed from the API.
Campbell Ritchie wrote:Somebody else said you could use new String("lock") for objects, and use those identical Strings as locks.
Jesper Young wrote:Interesting use case
Jesper Young wrote: Sometimes methods or classes are deprecated, but they are never actually removed from the API.
Pat Farrell wrote:I think in general, The Java Fathers should deprecate more and remove more. Java is showing its age.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koophttps://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton