About Question enthuware.ocpjp.v21.2.1724 :
Posted: Mon Jun 16, 2025 12:04 pm
"the implementation of compareTo methods of String, Integer, and Double classes are not compatible with each other and they throw ClassCastException when an object of one class is compared with an object of another class." this sentence in the explanation and actually the answer of the question, I strongly advice to add why they are not comparable to each other also. For example, Integer and Double are also not comparable when used together. The code group below would throw the same exception, too.
Object[] sa = { 100.1, 100.0, 100 };
Collections.sort(Arrays.asList(sa), null);
System.out.println(sa[0]+" "+sa[1]+" "+sa[2] );
Why is that exactly?
Object[] sa = { 100.1, 100.0, 100 };
Collections.sort(Arrays.asList(sa), null);
System.out.println(sa[0]+" "+sa[1]+" "+sa[2] );
Why is that exactly?