CKTIS / Java
Equality operator ==
The == operator returns boolean: it compares primitive values or reference identity. For String objects it does not compare contents; use equals for that.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
left == rightleft == right returns boolean. Numbers are compared after numeric promotion; boolean values are equal when they match. != negates equality, whereas = means assignment.
For compatible reference types, == checks whether references identify the same object or array, or are both null. It does not compare contents. For String, equals compares the character sequence. In the example, a and b are distinct objects with identical contents.
Comparing a wrapper with a primitive may require unboxing: boxed == 7 compares numbers. Unboxing null throws NullPointerException. Two wrappers compared with == undergo reference comparison, not value comparison.
For floating-point numbers, NaN is unequal even to itself, whereas 0.0 == -0.0 is true. Numeric promotion can lose precision. Do not treat a == b == c as a test that three values are equal.
Examples
public class EqualityDemo {
public static void main(String[] args) {
String a = new String("Java");
String b = new String("Java");
String same = a;
Integer boxed = 7;
System.out.println(7 == 7L);
System.out.println(a == b);
System.out.println(a == same);
System.out.println(a.equals(b));
System.out.println(a == null);
System.out.println(boxed == 7);
System.out.println(Double.NaN == Double.NaN);
System.out.println(0.0 == -0.0);
}
}Result
true false true true false true false true
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.