Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double vs double in java [duplicate]

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?

like image 435
Mahmoud Avatar asked Nov 11 '12 14:11

Mahmoud


People also ask

Is it better to use double or double in Java?

Double is a class. double is a key word used to store integer or floating point number. double is primitive and Double is wrapper class.

Is there a difference between double and double in Java?

Double is an object and double is a primitive data type. See this answer for more details. The Double class wraps a value of the primitive type double in an object.

Why is double not accurate in Java?

doubles are not exact. It is because there are infinite possible real numbers and only finite number of bits to represent these numbers.

What is double in Java?

Java double is used to represent floating-point numbers. It uses 64 bits to store a variable value and has a range greater than float type. Syntax: // square root variable is declared with a double type.


4 Answers

Double parameter can be null when double can't.

like image 79
Maxim Shoustin Avatar answered Sep 28 '22 08:09

Maxim Shoustin


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"
like image 30
Tom Cammann Avatar answered Sep 28 '22 07:09

Tom Cammann


- 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;
like image 37
Kumar Vivek Mitra Avatar answered Sep 28 '22 08:09

Kumar Vivek Mitra


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

like image 45
Igor Avatar answered Sep 28 '22 08:09

Igor