Guest User

How the compareTo() Method works

a guest
Aug 10th, 2015
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1.  
  2. public class CompareMethod {
  3.     public static void main(String[] args){
  4.         String a = "a";
  5.         String b = "b";
  6.         String A = "A";
  7.         String B = "B";
  8.         String ab = "ab";
  9.         String aa = "aa";
  10.         String cat = "cat";
  11.         String can = "can";
  12.         String dog = "dog";
  13.         int compareab;
  14.         int compareAB;
  15.         int compareabaa;
  16.         int comparecatcan;
  17.         int comparecatdog;
  18.         int compareAa;
  19.        
  20.         /* Give "a" a value of zero as a start of the alphabet and "z" a value of 25 to end the alphabet.
  21.          *
  22.          * Capital letters are applied in the same way. To determine a single letter's value comparable to that of another,
  23.          * including same case, just subtract that value of the last letter compared to that of the first letter in the compareTo lines.
  24.          *
  25.          * The letters that are compared are the first letters that are found different in the string.
  26.          *
  27.          * When comparing letters of a different case, there is a 32 magnitude difference in either way, depending on what is being subtracted
  28.          *
  29.          * In conclusion, all letters and their cases are given a numerical value to be "compared" when using the compareTo method
  30.          *
  31.          * Sorry for the scrappy code, I was using it to move stuff around and compare the compareTo method to other things :)
  32.          */
  33.        
  34.         System.out.println("Comparing String: " + a + " String: " + b);
  35.         compareab = "a".compareTo("b");
  36.         System.out.println(compareab); //Should output 1 "a - b"
  37.        
  38.         System.out.println("Comparing String: " + A + " String: " + B);
  39.         compareAB = "A".compareTo("B");
  40.         System.out.println(compareAB); //Should also output 1 "A - B"
  41.        
  42.         System.out.println("Comparing String: " + ab + " String: " + aa);
  43.         compareabaa = "ab".compareTo("aa");
  44.         System.out.println(compareabaa); //Should output -1 "ab - aa";
  45.        
  46.         System.out.println("Comparing String: " + cat + " String: " + can);
  47.         comparecatcan = "cat".compareTo("can");
  48.         System.out.println(comparecatcan); //Should output 6 "cat - can"
  49.        
  50.         System.out.println("Comparing String: " + cat + " String: " + dog);
  51.         comparecatdog = "cat".compareTo("dog");
  52.         System.out.println(comparecatdog); //Should output -1 "cat - dog"
  53.        
  54.         System.out.println("Comparing String: " + A + " String: " + a);
  55.         compareAa = "A".compareTo("a"); //Should output -25 "A - a"
  56.         System.out.println(compareAa);
  57.        
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment