Comparison and equality
What you will learn
Comparison expressions answer questions such as whether stock is sufficient or two values match. They produce a boolean. Java uses == for primitive equality and for reference identity; checking the contents of two strings requires a different operation.
public class Main { public static void main(String[] args) { int available = 12; int requested = 12; System.out.println(available >= requested); System.out.println(available > requested); System.out.println(available != requested); }}Expected output:
truefalsefalseExactly sufficient stock meets an inclusive comparison but not a strict one.
How it works
For numeric operands, <, <=, >, >=, == and != compare values after applicable numeric conversion. Boolean values support == and !=, not numeric ordering. For reference operands, == asks whether both refer to the same object or both are null; it does not inspect the object’s contents. A method is a named operation that can be called; String.equals(Object) compares a string with another object by string contents, including case. In the example, new String(...) constructs a distinct string object; constructors are explained later. Calling an instance method on null fails, so a known non-null literal can be the receiver. Floating-point NaN is an exceptional value: ordinary ordered comparisons and == with NaN are false, even against itself, while != is true. Do not use == as a general approximate comparison for calculated floating-point values.
public class Main { public static void main(String[] args) { String first = new String("Shelf A"); String second = new String("Shelf A"); System.out.println(first == second); System.out.println(first.equals(second)); }}Expected output:
falsetrueThe objects are distinct, but their text matches.
Check a boundary or unusual input
public class Main { public static void main(String[] args) { String status = null; System.out.println("ready".equals(status)); double missing = Double.NaN; System.out.println(missing == missing); System.out.println(missing != missing); }}Expected output:
falsefalsetrueA non-null string returns false for a null argument. NaN follows special floating-point comparison rules.
Common mistakes
Do not write a == b to compare string contents, even if it happens to return true for two identical literals. Literal sharing can hide the bug. Use a.equals(b) when a is known non-null, or call equals on a known non-null expected string.
Try it yourself
Print whether a stock level of 18 meets an order of 18 and whether the supplied status new String("packed") equals the expected text "packed". Repeat with stock 17 and a null status. Print two booleans per case.
Save each example independently as Main.java. Compile with javac Main.java and run with java Main. Local Java practice only; automatic ByteMind Java grading is not established.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Comparison and equality — knowledge check
Apply comparison and equality to examples and boundary conditions.
Which expression accepts exactly 10 available units for an order of 10?
Two separately constructed strings contain identical text. What does `==` test?
Which expression safely tests a possibly null String named code against `"OK"`?
What is `Double.NaN == Double.NaN`?
Which operator tests primitive inequality?