About Question enthuware.ocpjp.v7.2.1301 :
Moderator: admin
-
- Posts: 27
- Joined: Sat Aug 31, 2013 5:38 pm
- Contact:
About Question enthuware.ocpjp.v7.2.1301 :
It definitely compile with a warning !
The method set interest is not used ...
Thanks
The method set interest is not used ...
Thanks
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1301 :
I think you are talking about IDE generated warning. The javac compiler has nothing to do with it, which is what we care about for the purpose of this exam.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 20
- Joined: Tue Nov 04, 2014 1:13 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1301 :
Was this changed in Java 7?However, this behavior was changed in the TreeSet implementation recently and it throws a ClassCastException when you add the first element itself.
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1301 :
Not sure when exactly it was changed but for Java 7 the behavior is as described.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 20
- Joined: Tue Nov 04, 2014 1:13 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1301 :
Ok. Thanks!
-
- Posts: 5
- Joined: Fri Jan 15, 2016 8:49 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1301 :
Ideally, when you add the first element, since there is nothing to compare this element to, there should be no exception.
If the first element added is-not-a Comparable, there should definitely be a ClassCastException? Since TreeSet only takes objects that are-a Comparable?
If the first element added is-not-a Comparable, there should definitely be a ClassCastException? Since TreeSet only takes objects that are-a Comparable?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1301 :
A TreeSet is a Set and to satisfy the Set interface, it has to accept any Object as an argument to the add method. The method implementation can impose additional restriction (such as the argument must be a Comparable) only by checking the object (e.g. obj instanceof Comparable) and throwing an unchecked but this is a design decision. Some time prior to Java 7, they didn't do this. A TreeSet accepted any object. It threw an exception only when a comparison was required to be done.rianne wrote:Ideally, when you add the first element, since there is nothing to compare this element to, there should be no exception.
If the first element added is-not-a Comparable, there should definitely be a ClassCastException? Since TreeSet only takes objects that are-a Comparable?
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Sun Aug 05, 2018 5:41 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1301 :
I wonder if lack of compile time restriction of elements being Comparable is because of constructor with Comparator. If there was no such constructor generic type E could be bounded as E extends Comparable and this error would be moved to compile time.
Am I right?
Am I right?
Who is online
Users browsing this forum: No registered users and 60 guests