About Question enthuware.ocpjp.v7.2.1301 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
sinapse
Posts: 27
Joined: Sat Aug 31, 2013 5:38 pm
Contact:

About Question enthuware.ocpjp.v7.2.1301 :

Post by sinapse »

It definitely compile with a warning !
The method set interest is not used ...

Thanks

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1301 :

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

SepticInsect
Posts: 20
Joined: Tue Nov 04, 2014 1:13 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1301 :

Post by SepticInsect »

However, this behavior was changed in the TreeSet implementation recently and it throws a ClassCastException when you add the first element itself.
Was this changed in Java 7?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1301 :

Post by admin »

Not sure when exactly it was changed but for Java 7 the behavior is as described.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

SepticInsect
Posts: 20
Joined: Tue Nov 04, 2014 1:13 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1301 :

Post by SepticInsect »

Ok. Thanks!

rianne
Posts: 5
Joined: Fri Jan 15, 2016 8:49 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1301 :

Post by rianne »

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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1301 :

Post by admin »

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?
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.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

nowkarol
Posts: 5
Joined: Sun Aug 05, 2018 5:41 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1301 :

Post by nowkarol »

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?

Post Reply

Who is online

Users browsing this forum: No registered users and 60 guests