-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringComparisonEx.java
More file actions
33 lines (24 loc) · 963 Bytes
/
StringComparisonEx.java
File metadata and controls
33 lines (24 loc) · 963 Bytes
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
package string;
public class StringComparisonEx {
public static void main(String[] args) {
String s1 = "Hello";
String s2 = "Hello";
System.out.println("s1 == s2 -> " + (s1 == s2));
System.out.println("s1.equals(s2) -> " + s1.equals(s2));
String s3 = new String("Hello");
String s4 = new String("Hello");
System.out.println("s3 == s4 -> " + (s3 == s4));
System.out.println("s3.equals(s4) -> " + s3.equals(s4));
System.out.println(" s1 == s3 ->" + (s1 == s3));
System.out.println(" s1.equals(s3) ->" + s1.equals(s3));
System.out.println(s3 == "Hello"); // here "Hello" is string literal
// Compares two strings lexicographically ?
s1.compareTo(s4);
// intern will check if already "Hello" is in String Pool
// If yes then return reference
// if no then put value in String pool
String s5 = new String("Hello").intern();
System.out.println("s1==s5 -> " + (s1 == s5)); // true
System.out.println(s1.equals(s5));
}
}