xxxxxxxxxx
public class StringComparison {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "World";
// Comparing two strings using the equals() method
if (str1.equals(str2)) {
System.out.println("Strings are equal");
} else {
System.out.println("Strings are not equal");
}
// Comparing two strings using the compareTo() method
int result = str1.compareTo(str2);
if (result == 0) {
System.out.println("Strings are equal");
} else if (result < 0) {
System.out.println("str1 is less than str2");
} else {
System.out.println("str1 is greater than str2");
}
}
}
xxxxxxxxxx
System.out.println("hey".equals("hey")); //prints true
/*
always use .equals() instead of ==,
because == does the compare the string content but
loosely where the string is stored in.
*/
xxxxxxxxxx
// These two have the same value
new String("test").equals("test") // --> true
// ... but they are not the same object
new String("test") == "test" // --> false
// ... neither are these
new String("test") == new String("test") // --> false
// ... but these are because literals are interned by
// the compiler and thus refer to the same object
"test" == "test" // --> true
// ... string literals are concatenated by the compiler
// and the results are interned.
"test" == "te" + "st" // --> true
// ... but you should really just call Objects.equals()
Objects.equals("test", new String("test")) // --> true
Objects.equals(null, "test") // --> false
Objects.equals(null, null) // --> true
xxxxxxxxxx
******Java String compareTo()******
The Java String class compareTo() method compares the given
string with the current string lexicographically.
It returns a positive number, negative number, or 0.
___________________________________________
if s1 > s2, it returns positive number
if s1 < s2, it returns negative number
if s1 == s2, it returns 0
___________________________________________
xxxxxxxxxx
class scratch{
public static void main(String[] args) {
String str1 = "Nyello";
String str2 = "Hello";
String str3 = "Hello";
System.out.println( str1.equals(str2) ); //prints false
System.out.println( str2.equals(str3) ); //prints true
}
}
xxxxxxxxxx
In Java Strings, the == operator is used to check the reference of both the string objects.
Use .equals method instead:
str1.equals(str2)
xxxxxxxxxx
class Teststringcomparison3{
public static void main(String args[]){
String s1="ddd";
String s2="Sachin";
String s3=new String("Sachin");
System.out.println(s1==s2);//true (because both refer to same instance)
System.out.println(s1==s3);//false(because s3 refers to instance created in nonpool)
}
}
xxxxxxxxxx
class Teststringcomparison3{
public static void main(String args[]){
String s1="ddd";
String s2="Sachin";
String s3=new String("Sachin");
System.out.println(s1==s2);//true (because both refer to same instance)
System.out.println(s1==s3);//false(because s3 refers to instance created in nonpool)
}
}
xxxxxxxxxx
class Teststringcomparison3{
public static void main(String args[]){
String s1="ddd";
String s2="Sachin";
String s3=new String("Sachin");
System.out.println(s1==s2);//true (because both refer to same instance)
System.out.println(s1==s3);//false(because s3 refers to instance created in nonpool)
}
}
xxxxxxxxxx
class Teststringcomparison3{
public static void main(String args[]){
String s1="ddd";
String s2="Sachin";
String s3=new String("Sachin");
System.out.println(s1==s2);//true (because both refer to same instance)
System.out.println(s1==s3);//false(because s3 refers to instance created in nonpool)
}
}