16

Possible Duplicate:
Java : different double and Double in comparison

In a sample java program for one of my labs, I have two different methods taking Double and double parameters respectively.
How do I differentiate between them when passing arguments to them?

Community
  • 1
  • 1
Mahmoud
  • 231
  • 1
  • 4
  • 12

7 Answers7

34

Double parameter can be null when double can't.

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
  • Nah, My problems is that I want to call the methods. So, when I am passing arguments to them, I don't know which one it goes to. The one accepting double or the one accepting Double? – Mahmoud Nov 11 '12 at 14:42
  • @Mahmoud Always force it to use the primitive `double` version by forcing the Double to unbox into a `double` before passing the parameter. – Andrew T Finnell Nov 11 '12 at 14:47
  • @Mahmoud true, primitive doubles are generally best. – Maxim Shoustin Nov 11 '12 at 14:48
  • @Andrew I cannot in this case. It's part of the lab instructions. – Mahmoud Nov 11 '12 at 14:48
  • @Mahmoud It depends on the static type of the parameter. If it's a primitive type, it will convert to a `double` and call the overload with the primitive. If the static type of the parameter is the class `Double`, then it will call the overload with the boxed type. – millimoose Nov 11 '12 at 14:49
  • @Mahmoud well, if you have method (interface) that allowed to you set `Double`, it's means that you can provide `null` instead and no crash will happen. – Maxim Shoustin Nov 11 '12 at 14:51
  • @Mahmoud (For what it's worth, I find this type of thing in school assignments irritating. It'd be more responsible to first teach good practice, and only then make you drill minutiae and edge cases.) – millimoose Nov 11 '12 at 14:51
27

First off you need to understand the difference between the two types. double is a primitive type whereas Double is an Object.

The code below shows an overloaded method, which I assume is similar to your lab code.

void doStuff(Double d){ System.out.println("Object call"); }
void doStuff(double d){ System.out.println("Primitive call"); }

There are several ways you can call these methods:

doStuff(100);
doStuff(200d);
doStuff(new Double(100));

These calls will result in:

"Primitive call"
"Primitive call"
"Object call"
Tom
  • 15,798
  • 4
  • 37
  • 48
  • 1
    Thanks a lot. It was so helpful. Didn't know about 200d at all. – Mahmoud Nov 11 '12 at 16:58
  • 4
    I found the following info more useful when understanding the difference: double is a simple number of type double. Double is a class. It holds a single field of type double, but has built-in functions like .toString() and floatValue(), and can be extended. With a 'double' you'd need to do String.valueOf() to get its string, but with Double you can just to .toString(). – Mike in SAT Dec 16 '16 at 00:13
  • When to use the first and when to use the latter? – F 505 May 02 '18 at 16:23
4

- double is a primitive type, where as Double is a wrapper object.

- One of the most common use of Wrapper objects is with Collection .

Eg:

List<Double> d = new ArrayList<Double>();

- In Java 5 a mechanism called Autoboxing has been introduced to convert between the two directly.

Eg:

double d = 10.41;
Double wrapper = d;
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • Wrapper objects aren't used with generics, they were used before generics to store primitive types in collections. They're also used in other contexts that call for nullable types. (Notably entity IDs when doing ORM.) – millimoose Nov 11 '12 at 14:57
1

Double is reference type and double is value type.

The Double class wraps a value of the primitive type double in an object. An object of type Double contains a single field whose type is double." link

As @Fess mentioned and because Double is reference type it can be null.

If you want you can explictly convert from Double to double with .doubleValue() method and viceverrsa with new Double(1.0).

Also as @millimoose said:

You should use X.valueOf() instead of new X(). The valueOf methods are allowed to cache the boxing types to reduce memory use. (Not sure this is done for Doubles but it's a good habit to get into.)"

millimoose
  • 39,073
  • 9
  • 82
  • 134
Igor
  • 1,835
  • 17
  • 15
  • You should almost never need to do this explicit conversion anymore. Autoboxing will insert calls to `doubleValue` and `valueOf` for you based on the context. – millimoose Nov 11 '12 at 14:35
  • Yes, I know. I changed my answer to "If you want you...". This is almost the same problem as this http://stackoverflow.com/questions/12846624/serious-generics-issue-int-vs-integer-java/12846665#12846665 – Igor Nov 11 '12 at 14:37
  • 1
    Also I think you should use `X.valueOf()` instead of `new X()`. The `valueOf` methods are allowed to cache the boxing types to reduce memory use. (Not sure this is done for `Double`s but it's a good habit to get into.) – millimoose Nov 11 '12 at 14:39
  • Yup, i wrote my answer really quickly and I didnt put that. I will edit my answer and put your previous comment quoted on the end – Igor Nov 11 '12 at 14:42
1
// Method A
public static void foo(Double d) {...}

// Method B 
public static void foo(double d) {...}

Evidently, if you pass a Double object then Method A will be called; i.e. if you had something like:

Double d = new Double(1.0);

Further, if you pass a double literal you will call Method B. What's interesting is if you have something like

double d = new Double(1.0);

In this case Method B will also be called, because the type of d is double; the Double object gets unboxed to a double. On the same note, if you had:

Double d = 1.0;

then Method A would be called, because the type of d would be Double (the double-literal gets autoboxed to a Double).

arshajii
  • 127,459
  • 24
  • 238
  • 287
  • An important aside is that if you *need* to do this sort of differentiation, it's probably a code smell. Ideally one of those overloads should wrap around the other. (In fact, in any set of overloads most should probably ultimately delegate to one of them or a single private implementation method.) – millimoose Nov 11 '12 at 14:37
  • 2
    "double d= new Double(1.0)" is not a subclass/superclass issue. "d is an instantiation of Double" is not true. It's not an instantiation of Double. You are creating an Object Double, then unboxing it into a double. Essentially it's a useless statement. – Andrew T Finnell Nov 11 '12 at 14:42
  • "then Method A would be called, because the type of d would be double (and not Double)" - still wrong. `d` would be a `Double`, seeing as *that's how you declared it*. – millimoose Nov 11 '12 at 14:52
  • Autoboxing doesn't let a variable be of two types at once, or be of a different type than is declared. All the compiler does is automatically insert boxing and unboxing code. This means that `double d1 = new Double(1.0);` gets compiled as `double d1 = new Double(1.0).doubleValue();`. Conversely, `Double d2 = 1.0;` gets compiled to `Double d2 = Double.valueOf(1.0);`. *At no point* is `d1` of type `Double`, or `d2` of type `double`. – millimoose Nov 11 '12 at 14:55
  • @millimoose Thanks - that was a typo. – arshajii Nov 11 '12 at 15:03
  • @AndrewFinnell Thanks as well - I edited to reflect your comment. – arshajii Nov 11 '12 at 15:11
  • @millimoose & AndrewFinnell Thanx guys. Although it got a bit too complicated for me (I am a first year student of Software), still, I could manage to solve the problem. – Mahmoud Nov 11 '12 at 17:04
1

Double is a wrapper class while double is a primitive type like c/c++. As pointed out above, Double is mostly used in generics but also is useful anywhere there is a need for both numerical value and proper object encapsulation. In most cases the Double and double can be used interchangeably.

yungtrizzle
  • 73
  • 2
  • 6
0

What you have is an example of method overloading. The good part is that the compiler and the JVM will select the correct method automatically based on the type of the arguments that is used when you call the method.

matsev
  • 32,104
  • 16
  • 121
  • 156