aspose file tools*
The moose likes Beginning Java and the fly likes Comparator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Comparator" Watch "Comparator" New topic
Author

Comparator

Nicholas Carrier
Ranch Hand

Joined: Apr 14, 2005
Posts: 78
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.


Teaching yourself anything is always the cheapest way, but it definitely takes a lot of time and effort.<br /> <br />Thank you javaranch <a href="http://"http://faq.javaranch.com/view?HowToAskQuestionsOnJavaRanch"" target="_blank" rel="nofollow">Learn How to Ask Your Question</a> and be nice
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Craig Tyler
Ranch Hand

Joined: Jan 15, 2006
Posts: 52
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

Joined: Apr 14, 2005
Posts: 78
So why does it work with a String and not an Integer?
Craig Tyler
Ranch Hand

Joined: Jan 15, 2006
Posts: 52
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

Joined: Apr 14, 2005
Posts: 78
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
Sheriff

Joined: Sep 28, 2004
Posts: 18842
    
  40

but this does not create an Integer object


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



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Nicholas Carrier
Ranch Hand

Joined: Apr 14, 2005
Posts: 78
k, got it

thanks.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Nicholas Carrier
Ranch Hand

Joined: Apr 14, 2005
Posts: 78
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparator