Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 209 Software Development Equality and Comparisons.

Similar presentations


Presentation on theme: "Computer Science 209 Software Development Equality and Comparisons."— Presentation transcript:

1 Computer Science 209 Software Development Equality and Comparisons

2 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

3 Equality with == s1 = new Student("ken", 10); s2 = s1; System.out.println(s1 == s2); // Prints true s3 = new Student("ken", 10); System.out.println(s1 == s3); // Prints false Student object s1 s2 The two variables refer to the same, identical object Student object s3

4 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

5 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 ==

6 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

7 The equals Method s1 = new Student("ken", 10); s2 = s1; System.out.println(s1.equals(s2)); // Prints true s3 = new Student("ken", 10); System.out.println(s1.equals(s3)); // Prints true

8 The equals Method s1 = new Student("ken", 10); s2 = s1; System.out.println(s1.equals(s2)); // Prints true s3 = new Student("ken", 10); System.out.println(s1.equals(s3)); // 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); } …

9 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.

10 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

11 The Comparable Interface public interface Comparable { // Returns 0 if receiver equals other // Returns < 0 if receiver is less than other // Returns > 0 if receiver is greater than other public int 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.

12 The Comparable Interface public interface Comparable { // Returns 0 if receiver equals other // Returns < 0 if receiver is less than other // Returns > 0 if receiver is greater than other public int compareTo(T other); } public class Student implements Comparable { public int compareTo(Student other){ return this.name.compareTo(other.name); } … More strict than equals ; type errors are caught at compile time

13 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

14 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));


Download ppt "Computer Science 209 Software Development Equality and Comparisons."

Similar presentations


Ads by Google