Interview question
How do you compare Strings in Java? Explain ==, equals(), and compareTo(). Java में Strings को कैसे compare करते हैं? ==, equals(), compareTo() explain करें।
Answer
String s1 = "Hello";
String s2 = "Hello";
String s3 = new String("Hello");
System.out.println(s1 == s2); // true
System.out.println(s1 == s3); // false
System.out.println(s1.equals(s3)); // true
System.out.println("Hello".equalsIgnoreCase("hello")); // true
System.out.println("Apple".compareTo("Banana")); // negativeString Comparison:
1. == : Reference comparison
2. equals() : Value comparison
3. equalsIgnoreCase() : Case-insensitive
4. compareTo() : Lexicographical comparisonWas this answer clear?