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

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -