Comparison Operators
Lesson:
Main Points:
- Comparison operators all return a boolean result (true or false)
- There are three types of comparison: ordinal, object type, and
equality
- Ordinal comparisons test the relative value of numeric operands
- Object-type comparisons determine if the runtime type of an object is of
a particular type or a subclass of that particular type
- Equality comparisons test if two values are the same and may be applied
to values of non-numeric types
- The comparison operators include: >, <, >=, <=
- The instanceof operator tests the class of an object at runtime
- The operators == and != test for equality and inequality,
respectively
- To determine if two different objects contain the same value, the equals
method must be used (and defined in the class of the object)
For primitive types, comparison is really straight forward. So for ==, !=,
<, >, >=, and <= there is no real need for explanation. However,
some explanation of instanceof and the use of the equals() method is
necessary.
String Equality
Consider the following example:
String output = "";
String s1 = "hello";
String s2 = "hello";
if(s1 == s2) output = "EQUAL";
else output = "NOT EQUAL";
What do you think the value of output is at the end of these lines of code?
The correct answer is "NOT EQUAL" because s1 and s2 are NOT the same string
object. They may contain the same value, but they are not the same entity.
They are two different String objects which happen to contain the same
value. To find out if two different String object contain the same value you
must to the following:
String output = "";
String s1 = "hello";
String s2 = "hello";
if(s1.equals(s2)) output = "EQUAL";
else output = "NOT EQUAL";
Using the equals method allows us to find out if the contents of two String
objects are the same. So, output will contain the value EQUAL after these
lines have run.
When you create your own classes, you must create an equals method if you
plan on checking for equality between two instances of these classes.
Checking for equality can be complicated for classes which contain complex
data structures.
The instanceof Operator
To find out if two objects are instances of the same class you use the
instanceof operator. The following applet demonstrates the use of the
instanceof operator. Inspect the code to see how to use the instanceof
operator.
import java.awt.*;
import java.applet.*;
public class comp extends Applet{
sparrow s1, s2;
bird b[];
crow c1, c2;
public void init(){
setBackground(Color.yellow);
s1 = new sparrow("Sparrow 1");
s2 = new sparrow("Sparrow 2");
c1 = new crow("Crow 1");
c2 = new crow("Crow 2");
b = new bird[] { s1, c1, s2, c2 };
}
public void paint(Graphics g){
String output="";
for(int x = 0; x < b.length; x++){
output = b[x].name();
if(b[x] instanceof crow)
output += " CROW!";
else
output += " NO CROW!!!";
g.drawString(output, 20, 30+15*x);
}
}
}
class bird{
String id;
bird(String s){
id=s;
}
public String name(){
return id;
}
}
class sparrow extends bird{
sparrow(String s){
super(s);
}
}
class crow extends bird{
crow(String s){
super(s);
}
}
Assignment:
Create a simple applet which contains an array of ten Strings which are
names. In your paint method print a personalized greeting for each name in
the array. Use the equals method in a series of if/if-else statements to
print out personalized greetings. (There are simpler ways of doing this, but
make sure you do it the way specified here.)