generics - java passes toString() to a method -


i have following code

public class mamman14 {       public static void main(string[] args) {         // todo code application logic here     }      public class sortedpair < e extends comparable < e >> {         e max_element;         e min_element;          public sortedpair(e firstelement, e secondelemnt) throws illegalpair {             int compare_result = firstelement.compareto(secondelemnt);              if (compare_result == 0) {                 max_element = null;                 min_element = null;                 throw new illegalpair(firstelement.tostring(), secondelemnt.tostring());             } else if (compare_result > 0) {                 max_element = firstelement;                 min_element = secondelemnt;             } else {                 max_element = secondelemnt;                 min_element = firstelement;              }         }          public e getfirst() {             return max_element;         }          public e getsecond() {             return min_element;         }          @         override         public string tostring() {             return string.format("%s bigger %s.", getfirst(), getsecond());         }      }      public class illegalpair extends exception {         public illegalpair() {             super("elements must different!!");         }          public illegalpair(string element) {             super("elements must different!! \n equal " + element);         }          public illegalpair(string element1, string element2) {             super("elements must different!! \n elements " + element1 + "and" +                 element2 + "and equal.");         }      } } 

it's simple program compares 2 elements , sets them in max_element , min_element accordingly. have 2 questions:

when write public class sortedpair <e extends comparable<e>> mean sortedpair can receive comparable elements contains compareto method?

in line throw new illegalpair(firstelement.tostring(), secondelemnt.tostring()) create new object using tostring() method of elements, lets elements don't have tostring() method, being send?

thanks.

when write public class sortedpair <e extends comparable<e>> mean sortedpair can receive comparable elements contains compareto method?

it means can use implements comparable type argument, e.g.:

sortedpair<string> sp; //         ^--- must class implements `comparable` //              or interface extends `comparable` 

...which in turn means can use methods defined comparable on instances declared generic type e.

in line throw new illegalpair(firstelement.tostring(), secondelemnt.tostring()) create new object using tostring() method of elements, lets elements don't have tostring() method, being send?

that use tostring object, pretty boring. :-) docs do:

the tostring method class object returns string consisting of name of class of object instance, at-sign character `@', , unsigned hexadecimal representation of hash code of object. in other words, method returns string equal value of:

getclass().getname() + '@' + integer.tohexstring(hashcode()) 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -