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 expression
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
=
==
x + 7 == 2*y
answer == 'y'
Equal to
≠
!=
score != 0
answer != 'y'
Not equal to
>
>
time > limit
Greater than
≥
>=
age >= 21
Greater than or equal to
<
<
pressure < max
Less than
≤
<=
time <=limit
Less than or equal to
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
)
EXAMPLE
String s1;
.
.
.