• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

SCJP book doubt? page 553

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public int compareTO(DVDInfo d) {

return title.comapreTo(d.getTitle());
}



suppose for example we have

Birthday girl/Thriller/Nicole kidman
Original Sin/adventure/Angelina Jolie
Evil Dead/Horror/Campbell


what is the difference between title and d.getTitle , i guess both will have same objects. i want to understand how are the objects taken???\

can anyone help?
 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose for first value of DVDInfo d the value is

Birthday girl/Thriller/Nicole kidman

so d.getTitle() will return Birthday girl and
return title.comapreTo(d.getTitle());
title will also contain Birthday girl then its the same right??

i didnt understand the concept of
thisObject.compareTo(anotherObject)

here what is thisObject and what is anotherObject?
i mean are both BIRTHDAY GIRL??
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't have the book in front of me, but I'm assuming that:
  • compareTo(DVDInfo d) is a method inside of DVDInfo, and
  • DVDInfo has a field called "title"
  • If these assumptions are correct, then compareTo(DVDInfo d) is comparing "this" instance of DVDInfo to some other instance of DVDInfo referenced by "d". This comparison is done by comparing the titles of these two instances, where "title" is this.title, and d.getTitle represents the title of the other instance.

    Further, I would assume that "title" is a String, in which case it is String's compareTo method that is invoked for the comparison.
     
    pras
    Ranch Hand
    Posts: 188
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Mark,

    thisObject.compareTo(anotherObject)

    you mean to say thisobject and anotherObject always will have different values?

    for EXAMPLE:

    Birthday girl/Thriller/Nicole kidman
    Original Sin/adventure/Angelina Jolie
    Evil Dead/Horror/Campbell

    so suppose thisObject value is Birthday Girl so the value of anotherObject can never be Birthday Girl is it? it has to be either Original Sin or Evil Dead right?

    Then when does comparator come into picture? is it that it compares Instance by Instance?
     
    marc weber
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by prasanna sheregar:
    ...so suppose thisObject value is Birthday Girl so the value of anotherObject can never be Birthday Girl is it? it has to be either Original Sin or Evil Dead right?

    Then when does comparator come into picture? is it that it compares Instance by Instance?


    You certainly could compare an instance to itself. The method should just return 0 to indicate that the instances are "equal."

    A Comparable object uses the compareTo method to compare itself (this) to the instance provided as a parameter.

    A Comparator does not use itself. Instead, 2 references are provided to the compare method, and these are compared to each other.
     
    What? What, what, what? What what tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic