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

what does this statement mean in comparable implementation?

 
Ranch Hand
Posts: 94
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

what does i-((B)obj) means??? its a sort of confusing to me.who will call compareTo method and who will pass Object type argument to that method?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What the compareTo method wants to do is subtract the i value of obj from the i value of the object it's called on. But you can't just write i - obj.i, because obj is an Object reference, and i doesn't exist in the Object class. So we need to perform a "cast".

(B)obj is a cast statement. It converts the Object reference to a B reference, so you can access the members of the B class (i, in this case). Note that it doesn't change the object itself, so it will only work if the object really is a B. If it isn't you'll get a ClassCastException.

So i - obj.i becomes i - ((B)obj).i - with the extra brackets because the . has higher precedence than the cast.


By the way, the cast isn't necessary if you use generics (which you really should):


As to when it gets called - the most common reason would be that you're using a sort function such as Collections.sort(). A sort function can't sort objects unless it knows how to compare two objects. That's what we do when we implement Comparable - we're saying "when you compare two objects of this type, this is how we want you to do it".
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what does this line mean?As Matthew says, you should really use generics. Putting suppress instructions into your code can cause you to miss serious type errors which can cause Exceptions and cause your program to crash later on.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a subtle bug in this code.

Try to add this to fill the list:

And then carefully look at the sorted list. You'll notice it is not sorted correctly.
 
kiran kumar reddy
Ranch Hand
Posts: 94
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:What the compareTo method wants to do is subtract the i value of obj from the i value of the object it's called on. But you can't just write i - obj.i, because obj is an Object reference, and i doesn't exist in the Object class. So we need to perform a "cast".

(B)obj is a cast statement. It converts the Object reference to a B reference, so you can access the members of the B class (i, in this case). Note that it doesn't change the object itself, so it will only work if the object really is a B. If it isn't you'll get a ClassCastException.

So i - obj.i becomes i - ((B)obj).i - with the extra brackets because the . has higher precedence than the cast.


By the way, the cast isn't necessary if you use generics (which you really should):


As to when it gets called - the most common reason would be that you're using a sort function such as Collections.sort(). A sort function can't sort objects unless it knows how to compare two objects. That's what we do when we implement Comparable - we're saying "when you compare two objects of this type, this is how we want you to do it".



oh my god what a confusing topic for me. you said it compareTo() method is doing a subtraction of ivalue from obj(and here there is no i in Object class we here done casting, and casting made us to access i value of B class and y can't we make directly B class obj as a parameter to compareTo method? that we done it through generics rite?

it means we want to use the super class type as an argument when we won't use particular object type(like here B obj) as parameter rite, and when we won't use generics? yes you said it is not advisable to not using generics, fine.) and i value of the object its called on(here that object means which we get while executing new B(90), new B(910) etc rite?

and i can't got the difference of the value of i from obj and i value of the object its called on. how both vary?
when i put print statements in compareTo() method like below


the output is as follows.


i understood that the value which got for obj is due to overriding of string method. because for every class super class is Object class and and it has toString method.
my Question is
why is that the value of obj differing from i??

sorry if i annoy you with these questions. but these questions are eating my mind.
 
kiran kumar reddy
Ranch Hand
Posts: 94
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:And what does this line mean?As Matthew says, you should really use generics. Putting suppress instructions into your code can cause you to miss serious type errors which can cause Exceptions and cause your program to crash later on.


yes you are correct and yes it might cause exceptions and program crash. Actually i am following a book in which it has colletions API first and generics is the next topic of it. so i am not using generics as i am learning first topic, later on while learning generics i will get to know its usefulness because i already used collection API with out using generics and get to know its advantages so that it will remain in my for long time.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kiran kumar reddy wrote:why is that the value of obj differing from i??



You have two instances of the B class. You call compareTo() on one of the instances and pass the other instance as a parameter. So the i seen directly in the compareTo() method belongs to one instance, and the obj reference is a different instance so has a different value. It is sort of like doing this:
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kiran kumar reddy wrote: . . . i am following a book in which it has colletions API first and generics is the next topic of it. so i am not using generics . . .

But you are still wrong to use the suppress instruction.
If you can’t use generics yet, tolerate the type safety warnings.
 
kiran kumar reddy
Ranch Hand
Posts: 94
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ all

hi guys what i am not getting from this example is given below. help me to clear it out...

here i have added objects of class B to Arraylist. and when it encounters println statement it will print the elements in it. here toString() is overridded.

then when come compareTo() method is called? and who will pass Object type obj to it?

it(javadoc) says compareTo(....) will compare this referenced object with object passed to it. in my example program someone(?) is calling compareTo() method on this referenced object with an one more object passed to it. so here they are two objects involving. so here these two objects are comparing through this method at one instinct and the other instinct some other two object will get compared. okay fine all these mechanism is doone by whom here in my code there is no visible code to show that mechanism. mean how it got sorted. program flow
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The sort() method calls compareTo(), or aComparator#compare() method.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kiran kumar reddy wrote:mean how it got sorted. program flow


That's not how it works here. If you want to know how a sort works, SearchFirst (←click).

However, all you basically need to know is that all a proper sort program needs to know is how to order TWO objects; which is why Java created the java.lang.Comparable interface. The rest is down to the sort algorithm itself.

Winston
 
reply
    Bookmark Topic Watch Topic
  • New Topic