About Question enthuware.ocpjp.v7.2.1624 :
Moderator: admin
-
- Posts: 16
- Joined: Fri Sep 04, 2015 5:06 pm
- Contact:
About Question enthuware.ocpjp.v7.2.1624 :
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.
If the array 'sa' has objects other than String that don't implement comparable, java.lang.ClassCastException will be thrown at runtime.
-
- Site Admin
- Posts: 10404
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1624 :
No, sa can never have objects other than Strings.
-
- Posts: 16
- Joined: Fri Sep 04, 2015 5:06 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1624 :
I mean Animal[] sa= {new Animal("dog"), new Animal("cat"), new Animal("elephant")}
where Animal is a valid class with constructor that takes string
where Animal is a valid class with constructor that takes string
-
- Posts: 33
- Joined: Sat Feb 15, 2020 12:44 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1624 :
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[]?
-
- Site Admin
- Posts: 10404
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1624 :
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.
-
- Site Admin
- Posts: 10404
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1624 :
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.
-
- Posts: 1
- Joined: Sat Jun 28, 2025 9:21 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1624 :
Regarding this:
I think the docs are stating that pretty clearly:Although not documented in the JavaDoc API description, passing null doesn't cause a NullPointerException.
The methods of this class all throw a NullPointerException if the collections or class objects provided to them are null.
-
- Site Admin
- Posts: 10404
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1624 :
Note sure which class's JavaDoc are you looking but Collections.sort doesn't throw NPE if the second argument is null.
The code given in the question works fine:
String[] sa = { "charlie", "bob", "andy", "dave" };
Collections.sort(Arrays.asList(sa), null);
The code given in the question works fine:
String[] sa = { "charlie", "bob", "andy", "dave" };
Collections.sort(Arrays.asList(sa), null);
Who is online
Users browsing this forum: No registered users and 12 guests