String mystring1 = new String("Java Rocks");
String mystring2 = new String("Java Rocks");
boolean test = mystring1 == mystring2;
boolean test1 = mystring1.equals(mystring2);
Results: test is false, test1 is true
===========================================
String mystring1 = "Java Rocks";
String mystring2 = "Java Rocks";
boolean test = mystring1 == mystring2;
boolean test1 = mystring1.equals(mystring2);
Results: test is true, test1 is true
===========================================
For the first block of code, test returns false because the == operator compares whether the references refer to the same object or not and not the state of the object.
Can anyone enlighten me why test returns true for the second block of code? Doesn't this way of declaring strings the same as the former way?
String mystring2 = new String("Java Rocks");
boolean test = mystring1 == mystring2;
boolean test1 = mystring1.equals(mystring2);
Results: test is false, test1 is true
===========================================
String mystring1 = "Java Rocks";
String mystring2 = "Java Rocks";
boolean test = mystring1 == mystring2;
boolean test1 = mystring1.equals(mystring2);
Results: test is true, test1 is true
===========================================
For the first block of code, test returns false because the == operator compares whether the references refer to the same object or not and not the state of the object.
Can anyone enlighten me why test returns true for the second block of code? Doesn't this way of declaring strings the same as the former way?