• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparator

 
Nicholas Carrier
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question regarding using a comparator to compare an int variable of an object.

When I create the comparator like this

class TitleCompare implements Comparator<Disc> {

public int compare(Disc disc1, Disc disc2) {

return disc1.getTitle().compareTo(disc2.getTitle());

}

}


where getTitle() returns a string, everything compiles and runs fine.

However when I create a comparator like this

class YearCompare implements Comparator<Disc> {

public int compare(Disc disc1, Disc disc2) {

return disc1.getYear().compareTo(disc2.getYear());
}

}



where getYear() returns an int it doesn't compile because of this:

JukeBoxTester.java:84: int cannot be dereferenced
return disc1.getYear().compareTo(disc2.getYear());
^
1 error




The integer class has a compareTo method so I really have no idea what is going on.

Anyone have any idea (I'm sure it's simple )?

As always, Thanks ahead of time.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Integer class has a compareTo method, but you are comparing primitive int values. Either you must wrap the two int values as Integer values, or you have to implement the compareTo method in your class to compare primitive int values.
 
Craig Tyler
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if getTitle returns an int, it's returning a primitive, not an object. Thus, you cannot use compareTo as primitives don't have methods.

However, you could do the following:


With this, you call compareTo on an object and disc2.getYear() gets autowrapped into an Integer (1.5 only. Otherwise, you have to wrap it yourself)

EDIT: The following would also be legal:

[ January 29, 2006: Message edited by: Craig Tyler ]
 
Nicholas Carrier
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So why does it work with a String and not an Integer?
 
Craig Tyler
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not trying to do it with a String and an Integer. You're trying to do it with a String (which is an object) and an int (which is not).
 
Nicholas Carrier
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, so this is something I clearly misunderstood and didn't know it . If someone could point me to a place that would explain it to me that'd be great.

In this setup:

public class Disc {

String name = "bla";

public String getName() {

return name;

}

}


when I call getName() on a Disc object it's returning a string object, but under this setup:

public class Disc {

int aNumber = 7;

public int getNumber() {

return aNumber;

}

}


when I call getNumber() on a Disc object it returns a primative value not an Integer Object.

So this creates a String object

String x = "a string";


but this does not create an Integer object

int x = 7;

[ January 30, 2006: Message edited by: Nicholas Carrier ]
 
Henry Wong
author
Marshal
Pie
Posts: 21008
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but this does not create an Integer object


Correct. To create an Integer Object you need to do this...



Henry
 
Nicholas Carrier
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
k, got it

thanks.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Craig Tyler:
if getTitle returns an int, it's returning a primitive, not an object. Thus, you cannot use compareTo as primitives don't have methods.

However, you could do the following:


With this, you call compareTo on an object and disc2.getYear() gets autowrapped into an Integer (1.5 only. Otherwise, you have to wrap it yourself)

EDIT: The following would also be legal:


[ January 29, 2006: Message edited by: Craig Tyler ]


I think this is not the best way to implement the compareTo() method when comparing primitive values. (I think you have a handle on the difference between Objects and primitives now.) A common way to do this is by subtracting the two values:

Notice that this will fulfill the contract of the compareTo() method:

if disc1.getYear() == disc2.getYear() then the return value is zero
if disc1.getYear() < disc2.getYear() then the return value is negative
if disc1.getYear() > disc2.getYear() then the return value is positive

This also avoids the overhead of creating extra objects. Besides, I'm willing to bet that this is exactly how Integer.compareTo() is implemented anyway.

Layne
 
Nicholas Carrier
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya it is, or at least that's the way I read it. Thanks, I'm sure that'll work too and I think it looks nicer

Ya, it all came down to the fact that for some reason I never realized that a small I integer, as in an int, wasn't the same as a uppercase I Integer, and in a new Integer(). It should have been clear to me, but it clearly wasn't
[ January 31, 2006: Message edited by: Nicholas Carrier ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic