String s1="str";equals() is case-sensitive(lowercase or uppercase):
String s2="str";
if(s1.equals(s2))
System.out.println("Both Strings are equal");
String s1="str";In this case, it returns false.
String s2="STR";
if(s1.equals(s2))
System.out.println("Both Strings are equal");
String s1="str";
String s2="STR";
if(s1.equalsIgnoreCase(s2))
System.out.println("Sensitivity to ignored case. The two Strings are equal");
if("string1".length()=="string2".length())
System.out.println("string1 and string2 equal in size");
else if("string1".length()>" string2".length())
System.out.println("string1 is longer");
else
System.out.println("string2 is longer");
new String("test").equals(new String("test"));//--> trueThe "==" operator tests the equality of objects, here are a series of errors to avoid:
String s1="trial";The concatenation returns true because it is called during compilation, unlike substring() and replace() which are called at runtime and a new object will be created that does not equal the other side of the equation.
String s2="trial";
//they are the same object
s1==s2; --> false
//they are not the same object
s1 == "test"; --> false
//They reference the same object
"test" == "test"; --> true
//the concatenation is considered as a single object
"test" == "your"+"t"; --> true
//the substring() method generates a new object
"test" == "1test".substring(1); --> false
//the replace() method generates a new object
"test" == "Test".replace("T", "t"); --> false
Please disable your ad blocker and refresh the window to use this website.