Comparing Values with Relational Operators
A relational expression compares two values and produces
true or false. The examples in this lesson come from one game, which checks whether a player has earned enough points for the next level.Relational expressions
int points = 240;
int nextLevel = 250;
boolean needsMore = points < nextLevel;
System.out.println(needsMore);
Java evaluates the right side of the third line first. The expression
points < nextLevel asks whether 240 is less than 250, and its value is true. The variable needsMore stores that value, so the segment prints true, and neither number is stored in it.The equal case
Give the player exactly 250 points.
int points = 250;
int nextLevel = 250;
System.out.println(points > nextLevel);
System.out.println(points >= nextLevel);
The first line printed is
false and the second is true. For any two different values, > and >= give the same answer. They disagree only when the values are equal, since >= counts that case as true and > doesn't. The same holds for < and <=.So the rule "a player who reaches 250 points levels up" calls for
>=. A program written with > gives the right answer at 240 and at 260, and the wrong one at 250. A test at 250 itself catches it.Booleans and mixed types
The operators
== and != also compare boolean values.boolean hasKey = true;
boolean hasMap = true;
System.out.println(hasKey == hasMap);
This prints
true, since both variables hold the same value. Written with !=, it prints false. The four ordering operators compare numbers only, so hasKey < hasMap doesn't compile. Neither does a comparison between a number and a boolean, such as points == true.An
int and a double can be compared. Java converts the int to a double first, so points == 250.0 is true when points holds 250.Integer division and round-off
A comparison reads the values its operands have when it runs, and each operand is evaluated completely before the comparison happens.
int points = 250;
int rounds = 4;
System.out.println(points / rounds >= 62.5);
This prints
false. Both operands of the division are int values, so it's integer division, and points / rounds is 62 with the remainder discarded. Only then is 62 compared with 62.5. Writing points / 4.0 makes the division a double division with the value 62.5, and the comparison is true.A
double can also hold a value slightly different from the decimal it was meant to be, since most decimal fractions have no exact binary form.Comparing Strings
A
String variable holds a reference to a String object. Applied to two references, == is true when both lead to a single object.String rank = new String("silver");
String goal = new String("silver");
System.out.println(rank == goal);
System.out.println(rank.equals(goal));Each
new creates a separate object, so rank == goal is false, even though the two objects hold the same characters. The equals method compares the characters, and it returns true.Assignment and comparison
The single
= is assignment, and it changes a variable. The double == is a comparison, and it changes nothing.int points = 250;
System.out.println(points == 300);
System.out.println(points = 300);
The second line prints
false, and points still holds 250. The third line looks almost the same, but it's an assignment: it stores 300 in points, and the value it prints is the 300 just stored. One missing equal sign turned a question about points into a change to it.