Hi,
StudyGroup mathGroup = new StudyGroup();
mathGroup.add(new Person("MATH"));
System.out.println("A");
There are two answers:
1. It will compile without warning but will throw an exception at runtime.
2. It will only print : A
Answer 2 is not correct. Because add method will throw Exception
Exception in thread "main" java.lang.ClassCastException: main.java.Person cannot be cast to java.lang.Comparable
at java.util.TreeMap.compare(TreeMap.java:1188)
at java.util.TreeMap.put(TreeMap.java:531)
at java.util.TreeSet.add(TreeSet.java:255)
If look at the code of TreeMap, they will compare keys beetween each other
public V put(K key, V value) {
Entry<K,V> t = root;
if (t == null) {
compare(key, key); // type (and possibly null) check
So, explanation: "Thus, when you add the first element, since there is nothing to compare this element to, there is no exception. " also is not correct.
Pleach check. (jdk 1.7.0_02)
About Question enthuware.ocpjp.v7.2.1301 :
Moderators: Site Manager, fjwalraven
-
- Posts: 42
- Joined: Tue Feb 12, 2013 4:35 pm
- Contact:
-
- Posts: 42
- Joined: Tue Feb 12, 2013 4:35 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1301 :
Hi, any comments?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1301 :
We are still investigating. Apparently, the behavior changed recently. It used to work as given earlier.
-Paul.
-Paul.
-
- Posts: 42
- Joined: Tue Feb 12, 2013 4:35 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1301 :
Ok, my apologies.
I thought that you just missed my post.
Alex
I thought that you just missed my post.
Alex
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1301 :
Hi Alex,
In light of how it behaves currently, we have updated the question and the explanation.
thank you for your feedback!
Paul.
In light of how it behaves currently, we have updated the question and the explanation.
thank you for your feedback!
Paul.
Who is online
Users browsing this forum: Bing [Bot] and 16 guests