eh3rrera / ocpj8-book

Study guide for the Oracle Certified Professional, Java SE 8 Programmer Exam (1Z0-809)
Other
129 stars 90 forks source link

Chapter 16 confirmation #86

Open VishGov opened 5 years ago

VishGov commented 5 years ago

Hi Esteban,

The book has been great so far.

One quick confirmation I wanted regarding this comment in the Comparable interface code: // If the objects are equal, compare by id if(result == 0) { // Let's do the comparison "manually" // instead of using Integer.compareTo() if(this.id > other.id) result = 1; else if( this.id < other.id) result = -1; // else result = 0; }

The line in question is : // Let's do the comparison "manually" // instead of using Integer.compareTo()

Could you confirm that you actually meant Integer.compare(int a, int b) as compareTo is misleading [ the signature might be different ]

Please correct me if I am wrong here.

Thanks, Vishnu

eh3rrera commented 5 years ago

Hi Vishnu!

Actually, it can be both, Integer.compareTo(Integer i) or Integer.compare(int a, int b). According to the documentation, compare(int a, int b) is the same as:

Integer.valueOf(x).compareTo(Integer.valueOf(y))

Since that part of the chapter is about the Comparable interface, I used comparedTo. Do you think it would be better to change:

// instead of using Integer.compareTo()

To:

// instead of using Integer.valueOf(this.id).compareTo(other.id) or Integer.compare(this.id, other.id)

What do you think?

Thanks!

VishGov commented 5 years ago

Hi,

I think it should be changed to what you have mentioned in the last line. It's less ambiguous that way :)

Thanks, Vishnu

On Wed, 30 Oct 2019, 6:51 am Esteban Herrera, notifications@github.com wrote:

Hi Vishnu!

Actually, it can be both, Integer.compareTo(Integer i) or Integer.compare(int a, int b). According to the documentation https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#compare-int-int-, compare(int a, int b) is the same as:

Integer.valueOf(x).compareTo(Integer.valueOf(y))

Since that part of the chapter is about the Comparable interface, I used comparedTo. Do you think it would be better to change:

// instead of using Integer.compareTo()

To:

// instead of using Integer.valueOf(this.id).compareTo(other.id) or Integer.compare(this.id, other.id)

What do you think?

Thanks!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/eh3rrera/ocpj8-book/issues/86?email_source=notifications&email_token=ADFQTOEVZQFSNSGV7FOZOMTQRDOTJA5CNFSM4JGPPWP2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOECSTMWY#issuecomment-547698267, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADFQTOBFMPTIGK5WQ5FADVTQRDOTJANCNFSM4JGPPWPQ .

eh3rrera commented 5 years ago

Changed, thanks to you!