forked from mertsaner/java-interview-questions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompareStrings.java
More file actions
38 lines (31 loc) · 1 KB
/
CompareStrings.java
File metadata and controls
38 lines (31 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package strings;
public class CompareStrings {
public static void main(String[] args) {
String s1 = "Ram";
String s2 = "Ram";
String s3 = new String("Ram");
String s4 = new String("Ram");
String s5 = "Shyam";
String s6 = null;
String s7 = null;
try {
System.out.println("\nComparing String with equals(): ");
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s3.equals(s4));
//System.out.println(s6.equals(s7)); // NullPointerException
System.out.println("\nComparing String with ==:");
System.out.println(s1 == s2);
System.out.println(s1 == s3);
System.out.println(s1 == s4);
System.out.println(s6 == s7);
System.out.println("\nComparing String with compareTo():");
System.out.println(s1.compareTo(s3));
System.out.println(s1.compareTo(s5));
//System.out.println(s6.compareTo(s7)); // NullPointerException
} catch (Exception e) {
System.out.println("Exception: " + e);
e.printStackTrace();
}
}
}