2-Equality and Comparisons
Download
Report
Transcript 2-Equality and Comparisons
Computer Science 209
Software Development
Equality and Comparisons
Primitive Types vs Classes
• Primitive values (of type int, double, char,
etc.) recognize operators only (+, *, <, etc.)
• Objects (of type String, Student, etc.)
recognize methods and a very few operators
• The == operator means something very different
for int and String
Equality with ==
s1 = new Student("ken", 10);
s2 = s1;
System.out.println(s1 == s2);
s3 = new Student("ken", 10);
System.out.println(s1 == s3);
// Prints true
// Prints false
The two variables refer to the same, identical object
s1
Student
object
s2
s3
Student
object
What Is Equality?
• Object identity: two variables refer to the exact same object
• Structural equivalence: two variables refer to distinct objects that have
the same contents
• Object identity is pretty strict, maybe too strict
• == actually tests for object identity
• Use the equals method to test for structural equivalence
The equals Method
• Defined in the Object class (the root class of
Java’s class hierarchy), and behaves like == by
default
• Can be overridden in subclasses to support a test
for equality of structure instead
• Always use equals with strings; never use ==
The equals Method
public boolean equals(Object other)
The Object parameter can actually be of any
class when this method is called
This allows any object to be compared to any other
object for equality
The implementation will convert the type of the
Object parameter to its actual type by using a
cast operator
The equals Method
s1 = new Student("ken", 10);
s2 = s1;
System.out.println(s1.equals(s2));
s3 = new Student("ken", 10);
System.out.println(s1.equals(s3));
// Prints true
// Prints true
The equals Method
s1 = new Student("ken", 10);
s2 = s1;
System.out.println(s1.equals(s2));
s3 = new Student("ken", 10);
System.out.println(s1.equals(s3));
// Prints true
// Prints true
public class Student{
public boolean equals(Object other){
if (this == other)
return true;
if (! (other instanceof Student)) return false;
Student s = (Student) other;
return this.name.equals(s.name);
}
…
Comparisons
String s1 = "Apple";
String s2 = "Microsoft";
if (3 < 4) doSomething1();
if (s1.compareTo(s2) < 0) doSomething2();
The comparison operators work only with values
of primitive types.
We must use the method compareTo to compare
two objects.
The compareTo Method
• Returns 0 if the two objects are equal (using
equals)
• Returns an integer < 0 if the first object is less
than the second one
• Otherwise, returns an integer > 0
The Comparable Interface
public interface Comparable<T>{
// Returns
// Returns
// Returns
public int
0 if receiver equals other
< 0 if receiver is less than other
> 0 if receiver is greater than other
compareTo(T other);
}
An interface specifies a set of one or more
methods (headers only) that an implementing
classe must include.
T is a type variable, whose actual type is filled in
by an implementing class.
The Comparable Interface
public interface Comparable<T>{
// Returns
// Returns
// Returns
public int
0 if receiver equals other
< 0 if receiver is less than other
> 0 if receiver is greater than other
compareTo(T other);
}
public class Student implements Comparable<Student>{
public int compareTo(Student other){
return this.name.compareTo(other.name);
}
…
More strict than equals; type errors are caught at compile time
The java.util.Arrays Class
• Like the Math class, but includes class methods
for processing arrays
• Sorting, searching, find the maximum element,
etc.
• Assumes comparison operators for primitive
elements and compareto for elements that are
objects
Examples with Various Arrays
int[] ints = {50, 30, 100, 20};
java.util.Arrays.sort(ints);
java.util.Arrays.binarySearch(ints, 100);
// Returns 3
String[] names = {"Banana", "Apple", "Cherry"};
java.util.Arrays.sort(names);
java.util.Arrays.binarySearch(names, "ken"); // Returns -1
String[] students = new Student[10];
// Add 10 Student objects to the array
java.util.Arrays.sort(students);
java.util.Arrays.binarySearch(students,
new Student("ken", 10));