About Question enthuware.ocpjp.v11.2.1862 :

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

Moderator: admin

Post Reply
fawcette01
Posts: 1
Joined: Wed Feb 02, 2022 6:58 pm
Contact:

About Question enthuware.ocpjp.v11.2.1862 :

Post by fawcette01 »

What is going on here?
The list will not be printed, only the object references. Or should we assume there is a toString()?

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

Re: About Question enthuware.ocpjp.v11.2.1862 :

Post by admin »

There is no need to assume. All objects have a toString method already. It may not print the value of the fields of the object, but it does print a unique string for that object (No, it doesn't print object reference. It prints the hashcode).

Further, what the code actually prints is not really relevant here because all it says is that the list is printed, which it does. It doesn't claim anything about the actual format or content of what is printed.
If you like our products and services, please help us by posting your review here.

mansi29feb
Posts: 4
Joined: Thu Aug 04, 2022 10:23 am
Contact:

Re: About Question enthuware.ocpjp.v11.2.1862 :

Post by mansi29feb »

The book class is not implementing Comparable interface then how the compareTo function is working on line //1

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

Re: About Question enthuware.ocpjp.v11.2.1862 :

Post by admin »

Book doesn't implement Comparable. But the code passes a Comparator instance that is able to compare two Book objects.
This Comparator uses the compareTo method of String class.
Observe the lambda used to create the Comparator. It calls b1.getGenre().compareTo and not b1.compareTo.
If you like our products and services, please help us by posting your review here.

mansi29feb
Posts: 4
Joined: Thu Aug 04, 2022 10:23 am
Contact:

Re: About Question enthuware.ocpjp.v11.2.1862 :

Post by mansi29feb »

Thanks for the explaination. Got it.

Post Reply

Who is online

Users browsing this forum: No registered users and 63 guests