-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelationalOperators.java
More file actions
87 lines (78 loc) · 2.93 KB
/
Copy pathRelationalOperators.java
File metadata and controls
87 lines (78 loc) · 2.93 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package Phase1_CoreLanguage.Operators;
/**
* Relational (Comparison) Operators
* ---------------------------------
* Relational operators compare two values and produce a boolean result.
* <p>
*
* == equal to
* != not equal to
* > greater than
* < less than
* >= greater than or equal
* <= less than or equal
* <p>
*
* IMPORTANT - == on Objects vs Primitives
* ---------------------------------------
* - On PRIMITIVES, == compares VALUES.
* - On REFERENCE TYPES, == compares REFERENCES (do both variables point to the
* same object on the heap?). To compare logical equality of objects, use the
* .equals(...) method.
* <p>
*
* String a = new String("hi");
* String b = new String("hi");
* a == b -> false (different objects)
* a.equals(b) -> true (same content)
* <p>
*
* NaN Comparisons
* ---------------
* NaN (Not-a-Number) is unique - it is NOT equal to itself:
* Double.NaN == Double.NaN -> false
* Double.NaN != Double.NaN -> true
* Double.NaN > 0 -> false
* Double.NaN < 0 -> false
* Use Double.isNaN(value) to test for NaN safely.
*/
public class RelationalOperators {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println("a == b ? " + (a == b)); // false
System.out.println("a != b ? " + (a != b)); // true
System.out.println("a > b ? " + (a > b)); // false
System.out.println("a < b ? " + (a < b)); // true
System.out.println("a >= 10? " + (a >= 10)); // true
System.out.println("b <= 20? " + (b <= 20)); // true
// Reference vs content comparison for objects
String s1 = new String("hello");
String s2 = new String("hello");
String s3 = s1; // alias - same reference
System.out.println("s1 == s2 ? " + (s1 == s2)); // false
System.out.println("s1 == s3 ? " + (s1 == s3)); // true
System.out.println("s1.equals(s2) ? " + s1.equals(s2)); // true
// String literal pool - literals share the same reference
String x = "java";
String y = "java";
System.out.println("\"java\"==\"java\"? " + (x == y)); // true (pooled)
// NaN comparisons
double nan = Double.NaN;
System.out.println("nan == nan ? " + (nan == nan)); // false (!)
System.out.println("Double.isNaN(nan)? " + Double.isNaN(nan)); // true
// OUTPUT
// a == b ? false
// a != b ? true
// a > b ? false
// a < b ? true
// a >= 10? true
// b <= 20? true
// s1 == s2 ? false
// s1 == s3 ? true
// s1.equals(s2) ? true
// "java"=="java"? true
// nan == nan ? false
// Double.isNaN(nan)? true
}
}