2
import java.awt.Rectangle;
import java.util.Comparator;
public class RectangleComparator implements Comparator
{
      public int compare(Object object1, Object object2)
      {
        Rectangle rec1 = (Rectangle) object1;
        Rectangle rec2 = (Rectangle) object2;

        return rec1.getWidth().compareTo(rec2.getWidth());
    }
}

For some reason, I'm getting the error double cannot be dereferenced. Can anyone help me figure out why?

user2760309
  • 73
  • 1
  • 4
  • BTW, it's rarely a good idea to compare float/double values for exact equality. See http://stackoverflow.com/questions/4915462/how-should-i-do-floating-point-comparison – sleske Sep 09 '13 at 05:00

3 Answers3

5

To compare two double primitives:

Java old school:

return new Double(rec1.getWidth()).compareTo(new Double(rec2.getWidth());

Java 1.4 onwards:

return Double.compare(rec1.getWidth(), rec2.getWidth());
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • 1
    +1 for adding the newer Java7 version which is much more efficient that creating Double wrappers. – user949300 Sep 09 '13 at 05:12
  • 1
    Can you explain or link as to why this is different? – David says Reinstate Monica Sep 09 '13 at 05:15
  • While the second one is much cleaner and preferable is it really more efficient? – Thihara Sep 09 '13 at 05:18
  • @Thihara The first one creates two new Double objects. The second does math on the double values directly. I'm confused where the Java 7 part comes in, though, as the javadocs seem to say Double.compare(double, double) has been around since 1.4 – James Sep 09 '13 at 05:20
  • @Bohemian, What is so wrong in converting a value to string and comparing them for equality? My answer was voted down but I found another answer [here](http://stackoverflow.com/a/10274108/295264) suggesting the same thing on his last option. – Starx Sep 09 '13 at 05:28
  • @Starx We aren't (just) comparing equality here - it's a comparator. You're defining an ordering. Converting to a string changes the ordering from the natural ordering of doubles to a string based ordering (e.g. 100 comes before 11) – James Sep 09 '13 at 05:35
  • @James, Oh.. Now I get it. Thanks – Starx Sep 09 '13 at 05:41
  • The "Java 7" version has been around since Java 1.4. – Louis Wasserman Sep 09 '13 at 05:59
  • @LouisWasserman I don't know why I thought that. Thanks for commenting (answer corrected) – Bohemian Sep 09 '13 at 06:31
  • Yeah... I thought it has been around. – David says Reinstate Monica Sep 09 '13 at 14:48
4

rocketboy is correct with regard to why this is happening.

Consider using the Double wrapper class like this

new Double(rec1.getWidth()).compareTo(rec2.getWidth());

Only the first value need to be converted to a wrapper Double, the second one will be auto boxed.

Thihara
  • 7,031
  • 2
  • 29
  • 56
  • 1
    You should not use `new Double(…)` at all. Please get into the habit of using `Double.valueOf(…)` as this will allow Java to reuse existing objects. Of course that’s even more important for small integers or longs. – Michael Piefel Sep 09 '13 at 06:39
  • @MichaelPiefel Does this depend on the implementation or am I missing something? The `Double.valueOf(double d)` seem to be just calling new `Double(double d)` under the wraps. – Thihara Sep 09 '13 at 07:15
  • That’s correct (for my Oracle JDK) – but it just _might_ be better, as the comment there suggests. It’s actually implemented for integers and longs between -128 and 127. I wonder if there really is an implementation that caches more and whether that is actually more efficient in any way. – Michael Piefel Sep 09 '13 at 08:54
  • I checked in the openjdk 1.6 and 1.7. There's no way caching is implemented at their current state. – Thihara Sep 09 '13 at 08:57
3

I think your Rectangle.getWidth() returns a double. It is not a wrapper Object like Double, so the dot operator cannot be used.

Had it been: Double getWidth() instead of double getWidth()

then rec1.getWidth().compareTo(rec2.getWidth()); would have been valid.

How to compare doubles in Java?

Community
  • 1
  • 1
rocketboy
  • 9,573
  • 2
  • 34
  • 36