Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Comparable and compareTo

 
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from KS & BB book..


Please let me know the exact relationship in-between Comparable<DVDInfo> and compareTo(DVDInfo d). How should i read them?
As whenever i try to differ the Comparable's concrete type and compareTo() methods argument, it gives a compile time error.

This is what given in the book: "In line 1 we declare that class DVDInfo implements Comparable in such a way
that DVDInfo objects can be compared to other DVDInfo objects
". which is turning out to be an insufficient information for me..
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@swaraj
what you are trying to implement using different types is unknown to us
so please post it here

and
about line 1
that surely says that, we can compare objects of type DVDInfo

what you have written is correct , DVDInfo objects getting compared means that
we can determine whether two DVDInfo objects are equal or less than or greater than with each other on the basis of its instance variable
for example



here,
compareTo() method is called whenever we sort the array of the DVDInfo objects and that sorting order is determined using the price

hope this is clear to you now

and post your code please
hth
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what I was trying to do...as i didn't get the point...
case 1:

case 2:
Please let me know the problem with these cases.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the interface definition is:
So, if you implement Comparable<DVDInfo>, the interface definition tells you that there should be a method compareTo(DVDInfo). Anything else and you'll get a compiler error (unless it's an abstract class) because you simply haven't implemented the interface. As far as the compiler is concerned, it's exactly the same situation as if you'd omitted the compareTo method entirely.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic