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.

Online
admin
Site Admin
Posts: 10403
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.

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[]?

Online
admin
Site Admin
Posts: 10403
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.

Online
admin
Site Admin
Posts: 10403
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.

lucavassos
Posts: 1
Joined: Sat Jun 28, 2025 9:21 am
Contact:

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

Post by lucavassos »

Regarding this:
Although not documented in the JavaDoc API description, passing null doesn't cause a NullPointerException.
I think the docs are stating that pretty clearly:
The methods of this class all throw a NullPointerException if the collections or class objects provided to them are null.

Post Reply

Who is online

Users browsing this forum: admin and 13 guests