19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

574 Chapter 15 Abstract Classes and Interfaces<br />

public class String extends Object<br />

implements Comparable<br />

// class body omitted<br />

{<br />

public class Date extends Object<br />

implements Comparable<br />

// class body omitted<br />

{<br />

}<br />

@Override<br />

public int <strong>com</strong>pareTo(String o) {<br />

// Implementation omitted<br />

}<br />

}<br />

@Override<br />

public int <strong>com</strong>pareTo(Date o) {<br />

// Implementation omitted<br />

}<br />

Thus, numbers are <strong>com</strong>parable, strings are <strong>com</strong>parable, and so are dates. You can use the<br />

<strong>com</strong>pareTo method <strong>to</strong> <strong>com</strong>pare two numbers, two strings, and two dates. For example, the<br />

following code<br />

displays<br />

1 System.out.println(new Integer(3).<strong>com</strong>pareTo(new Integer(5)));<br />

2 System.out.println("ABC".<strong>com</strong>pareTo("ABE"));<br />

3 java.util.Date date1 = new java.util.Date(2013, 1, 1);<br />

4 java.util.Date date2 = new java.util.Date(2012, 1, 1);<br />

5 System.out.println(date1.<strong>com</strong>pareTo(date2));<br />

-1<br />

-2<br />

1<br />

Line 1 displays a negative value since 3 is less than 5. Line 2 displays a negative value since<br />

ABC is less than ABE. Line 5 displays a positive value since date1 is greater than date2.<br />

Let n be an Integer object, s be a String object, and d be a Date object. All the following<br />

expressions are true.<br />

n instanceof Integer<br />

n instanceof Object<br />

n instanceof Comparable<br />

s instanceof String<br />

s instanceof Object<br />

s instanceof Comparable<br />

d instanceof java.util.Date<br />

d instanceof Object<br />

d instanceof Comparable<br />

Since all Comparable objects have the <strong>com</strong>pareTo method, the<br />

java.util.Arrays.sort(Object[]) method in the <strong>Java</strong> API uses the <strong>com</strong>pareTo<br />

method <strong>to</strong> <strong>com</strong>pare and sorts the objects in an array, provided that the objects are instances of<br />

the Comparable interface. Listing 15.8 gives an example of sorting an array of strings and an<br />

array of BigInteger objects.<br />

create an array<br />

sort the array<br />

create an array<br />

LISTING 15.8<br />

SortComparableObjects.java<br />

1 import java.math.*;<br />

2<br />

3 public class SortComparableObjects {<br />

4 public static void main(String[] args) {<br />

5 String[] cities = {"Savannah", "Bos<strong>to</strong>n", "Atlanta", "Tampa"};<br />

6 java.util.Arrays.sort(cities);<br />

7 for (String city: cities)<br />

8 System.out.print(city + " ");<br />

9 System.out.println();<br />

10<br />

11 BigInteger[] hugeNumbers = {new BigInteger("2323231092923992") ,

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!