Equality operators
Equality operators compare two values for equality or inequality and return a boolean result.
| Operator | Description | Example |
|---|---|---|
== | Equal to | a == b |
!= | Not equal to | a != b |
With primitives: Compare actual values.
int x = 5, y = 5;System.out.println(x == y); // trueWith objects: == compares references (whether they point to the same object), not content.
String s1 = "hello";String s2 = "hello";String s3 = new String("hello");
System.out.println(s1 == s2); // true (same string literal pool)System.out.println(s1 == s3); // false (different objects)System.out.println(s1.equals(s3)); // true (content comparison)Important: Always use .equals() for object content comparison, unless you specifically want reference equality.
With null: You can check for null using == or !=.
String str = null;if (str == null) { System.out.println("Null reference");}