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.
*/
******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
___________________________________________
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)
}
}
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)
}
}
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)
}
}
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)
}
}
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)
}
}
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)
}
}
class Main {
public static void main(String[] args) {
// create 3 strings
String first = "java programming";
String second = "java programming";
String third = "python programming";
// compare first and second strings
boolean result1 = first.equals(second);
System.out.println("Strings first and second are equal: " + result1);
// compare first and third strings
boolean result2 = first.equals(third);
System.out.println("Strings first and third are equal: " + result2);
}
}