Чем отличается double от double java

Double class vs double primitive data type

Now, I know there are some manipulation what we can do With Double. JavaDoc but most (85%) of these methods are static there fore it not a huge advantage. Another thing I know that double cannot be null.

Double doubleClass = null; // Is valid double doubleType = null; // Results in NPE 

After all these disadvantages I cannot understand why we would use Double and not double in real life scenario. Can some give a real world example and explain.

3 Answers 3

There are two main reasons as far as I can see.

  1. There are libraries which simply don’t work with primitives. (The Collections API is the one you’re most likely to encounter soon but anything that uses generics will be similar).
  2. Exactly the fact that Double is a reference type and therefore can be null . Sometimes that information is important. (Imagine a scenario when a user can fill in various fields in a form, some are optional and you want to find out whether a field was filled in. A null value is a reasonably good indicator that a form field was left empty.)

But in general you’re right, whenever possible, you should use the primitive type.

There are many main reasons that you should use Double

  1. The Collections API that need to use the class and not primitive type
  2. Some cases you know that double can have varying precision if the come from different data source. You can use the class .equals note most of the cases
double d1,d2; // Initalized to some value d1==d2; // will return true but some(very few will return false) 

You should use the primitive type whenever possible and not Double.

Double is a wrapper class over the primitive double. The Java Collection framework has containers like List, Set etc. The problem with those containers is that they cannot accept primitives. So, double is implicitly converted to Double and inserted into the collections (Autoboxing).

Double doubleClass = null; // Is valid --> Double is not a primitive. So, you can get NPE if you do doubleClass.someInstanceLevelFieldOrMethod. double doubleType = 5.5; //is a primitive 

Also, Double will be slower than double because there is some book-keeping / overhead for Classes unlike primitives.

PS : Double provides many APi / methods to do things. example — Double.parseDouble(TypeX value) Creates a new double from value of TypeX

Источник

Double vs double in java [duplicate]

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?

7 Answers 7

Double parameter can be null when double can’t.

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 Always force it to use the primitive double version by forcing the Double to unbox into a double before passing the parameter.

@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.

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)

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" 

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().

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

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

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

double d = 10.41; Double wrapper = d; 

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.)

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) .

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.)»

Источник

Читайте также:  Www d color ru service html
Оцените статью