View Single Post
  #2 (permalink)  
Old 12-18-2007, 01:16 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
You can use the compareTo method for class instances/objects that implement the Comparable interface, egs, String and Integer.
Code:
String s1 = "hello world"; String s2 = "goodbye"; int retVal = s1.compareTo(s2); boolean equal = s1.equals(s2); String s3 = "95"; String s4 = "101"; // Which number is larger? Integer i3 = Integer.valueOf(s3); int n4 = Integer.parseInt(s4); Integer i4 = Integer.valueOf(n4); int compare = i3.compareTo(i4); System.out.println("compare = " + compare); System.out.printf("i3 is %s i4%n", (compare < 1) ? "less than" : (compare > 1) ? "greater than" : "equal to"); int n3 = i3.intValue(); System.out.printf("n3 < n4 = %b%n", n3 < n4);
Reply With Quote