Comparing Values:
Assignment
Statement: a single equal sign. For example, x = 5;
Comparing (Equal) Statement: a set of double equal
signs. For example, 10 ==
10;
Comparing (NOT EQUAL): For example, 12
!=
10;
Comparing Floating-Point Numbers
Consider this code
double r =
Math.sqrt(2);
double d =
r * r - 2;
if (d == 0)
//don't use
==
to compare floating-point numbers
System.out.println("sqrt(2)squared minus 2 is 0");
else
System.out.println("sqrt(2)squared minus 2 is not 0, but " + d);
It prints:
sqrt(2)squared minus 2 is not 0 but 4.440892098500626E-16
To avoid round off errors, don't use == to compare floating-point numbers
To compare
floating-point numbers test whether they are
close enough
|x
-
y|
≤ ε where ε is a
small number such as 10-14
final double EPSILON = 1E-14;
if (Math.abs(x - y) <= EPSILON)
// x is approximately equal to y
Comparing Strings
Don't use == for strings!
if (input == "Y") // WRONG!!!
Use equals method:
if (input.equals("Y"))
== tests identity, equals tests equal contents
public class Tester
{
public static void main(String [] args)
{
String myname = "Fredrick";
String hisname = myname.substring(0,4);
String nickname = "Fred";
System.out.println("Test by == ");
if( hisname == "Fred" ) //This result is wrong!!!
System.out.println("his name is Fred.");
else
System.out.println("his name is not Fred. \n");
if( nickname == "Fred" )
System.out.println("his nickname is Fred.\n");
else
System.out.println("his nickname is not Fred. \n");
System.out.println("Test by equals method ");
if(hisname.equals("Fred"))
System.out.println("his name is Fred.\n");
else
System.out.println("his name is not Fred.\n");
if(nickname.equals("Fred"))
System.out.println("his nickname is Fred.\n");
else
System.out.println("his nickname is not Fred. \n");
}
}
|
![]() |
Case insensitive test ("Y" or "y")
if (input.equalsIgnoreCase("Y"))
s.compareTo(t) < 0
means:
s
comes before
t
in the dictionary
"car" comes before "cargo"
All uppercase letters come before lowercase: "Hello" comes before "car"
Comparing Objects
== tests for identity, equals for identical content
Rectangle
box1 = new Rectangle(5, 10, 20, 30);
Rectangle
box2 = box1;
Rectangle
box3 = new Rectangle(5, 10, 20, 30);
box1
!= box3,
but
box1.equals(box3)
box1
== box2
Use equals to test whether two rectangles have the same contents.
If you have your own class, you have to implement your own equals method. It does not automatically exist.
Testing for null
null
reference refers to no object
String
middleInitial = null; // Not set
if ( . . . )
middleInitial = middleName.substring(0, 1);
Can be used in
tests:
if
(middleInitial == null)
System.out.println(firstName + " " + lastName);
else
System.out.println(firstName + " " + middleInitial + ". " + lastName);
Use ==, not equals, to test for null
null is not the same as the empty string ""
What is the value of s.length() if s is
the empty string ""?
the string " " containing a space?
null?
Answer: (a) 0; (b) 1; (c) an exception is thrown.
Which of the
following comparisons are syntactically incorrect?
Which of them are syntactically correct, but logically questionable?
String a = "1";
String b = "one";
double x = 1;
double y = 3 * (1.0 / 3);
a == "1" use equals method
a == null
a.equals("")
a == b use equals method
a == x type not match
x == y round off error
x - y == null type not match
x.equals(y) no equals method for double type
Answer: Syntactically incorrect: e, g, h. Logically questionable: a, d, f.