Java Reference
In-Depth Information
PITFALL: Using
=
in Place of
==
Because the equal sign,
=
, is used for assignment in Java, something else is needed to
indicate equality. In Java, equality is indicated with two equal signs with no space
between them, as in
if
(yourScore == myScore)
System.out.println("A tie.");
Fortunately, if you do use
=
in place of
==
, Java will probably give you a compiler
error message. (The only case that does not give an error message is when the expres-
sion in parentheses happens to form a correct assignment to a
boolean
variable.)
■
Display 3.3
Java Comparison Operators
MATH NOTATION
NAME
JAVA NOTATION
JAVA EXAMPLES
=
Equal to
==
x + 7 == 2*y
answer == 'y'
≠
Not equal to
!=
score != 0
answer != 'y'
>
Greater than
>
time > limit
≥
Greater than or equal to
>=
age >= 21
<
Less than
<
pressure < max
≤
Less than or equal to
<=
time <= limit
The Methods
equals
and
equalsIgnoreCase
When testing strings for equality, do not use
==
. Instead, use either
equals
or
equalsIgnoreCase
.
SYNTAX
String
.equals(
Other_String
)
String
.equalsIgnoreCase(
Other_String
)