Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning an integer literal to a double variable in Java

If I do the following

double d = 0;

since 0 is an integer literal, which uses 32 bits, and d is a double variable that uses 64 bits, will the remaining 32 bits be filled with random garbage, or does Java promote the literal correctly?

like image 443
cangrejo Avatar asked Feb 09 '12 13:02

cangrejo


People also ask

Can you assign an integer to a double Java?

We can convert int to double in java using assignment operator. There is nothing to do extra because lower type can be converted to higher type implicitly. It is also known as implicit type casting or type promotion.

What happens when int is assigned to double?

When one operand is an int and the other is a double, Java creates a new temporary value that is the double version of the int operand. For example, suppose that we are adding dd + ii where ii is an int variable. Suppose the value of ii is 3. Java creates a temporary value 3.0 that is the corresponding double value.

How do you assign a double variable in Java?

To declare (create) a variable, you will specify the type, leave at least one space, then the name for the variable and end the line with a semicolon ( ; ). Java uses the keyword int for integer, double for a floating point number (a double precision number), and boolean for a Boolean value (true or false).

Can integer be cast to double?

You cannot directly cast an Integer to a Double object. Also Double and Integer are immutable objects, so you cannot modify them in any way. Each numeric class has a primitive alternative ( Double vs double , Integer vs int , ...).


2 Answers

Java promotes it correctly, otherwise there'd be a rather large body of code that was problematic :-)

Section 5.1.2 of the Java language spec details this:

The following 19 specific conversions on primitive types are called the widening primitive conversions:

byte to short, int, long, float, or double
short to int, long, float, or double
char to int, long, float, or double
int to long, float, or double
long to float or double
float to double 

Widening primitive conversions do not lose information about the overall magnitude of a numeric value. Indeed, conversions widening from an integral type to another integral type and from float to double do not lose any information at all; the numeric value is preserved exactly. Conversions widening from float to double in strictfp expressions also preserve the numeric value exactly; however, such conversions that are not strictfp may lose information about the overall magnitude of the converted value.

Conversion of an int or a long value to float, or of a long value to double, may result in loss of precision-that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode.

Converting from a 32-bit Java int to a double (which, in Java, has 50+ bits of precision), will not lose the magnitude or any precision. If the constant you use is forced to a long due to its value, you may lose precision, since a long has 64 bits of precision.

like image 192
paxdiablo Avatar answered Nov 05 '22 14:11

paxdiablo


java automatically converts double to integer.ie upcast
so memory is automatically managed by jvm.
but it can not automatically cast double to integer ie.downcast.

like image 21
Abhij Avatar answered Nov 05 '22 15:11

Abhij