About Question enthuware.ocpjp.v7.2.1624 :

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

Moderator: admin

Post Reply
boyonsea
Posts: 16
Joined: Fri Sep 04, 2015 5:06 pm
Contact:

About Question enthuware.ocpjp.v7.2.1624 :

Post by boyonsea »

Just wanted to note that,
If the array 'sa' has objects other than String that don't implement comparable, java.lang.ClassCastException will be thrown at runtime.

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

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

Post by admin »

No, sa can never have objects other than Strings.
If you like our products and services, please help us by posting your review here.

boyonsea
Posts: 16
Joined: Fri Sep 04, 2015 5:06 pm
Contact:

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

Post by boyonsea »

I mean Animal[] sa= {new Animal("dog"), new Animal("cat"), new Animal("elephant")}

where Animal is a valid class with constructor that takes string

bvrulez
Posts: 33
Joined: Sat Feb 15, 2020 12:44 am
Contact:

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

Post by bvrulez »

What I don't understand is why "sa" is effected by "Comparator.sort(Arrays.asList(sa), null)". I would expect "Arrays.asList(sa)" to generate a NEW ArrayList (or something similarly appropriate extending List) which will get lost because it is not saved to a reference variable. Why does "Arrays.asList()" sort the underlying String[]?

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

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

Post by admin »

Because the list is backed by the array as explained in the [url=https://docs.oracle.com/javase/8/docs/a ... l#asList-T...-]JavaDoc API description of Arrays.asList method[/b]. Anything you do to the list actually happens on the underlying array.
If you like our products and services, please help us by posting your review here.

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

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

Post by admin »

One important habit that the part 1 exam (i.e OCA) inculcates is to read the JavaDoc. That should be your first source of information.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 80 guests