Java int and double types

Java int variables and literals represent integer values in the range -2147483648 to 2147483647 (inclusive.)

The limitation for Java int values is range. Integer overflow is a concern. See the Integer overflow section of Data representation using binary.

Java double variables and literals represent floating point values (values with decimals) in the range 4.9E-324 to 1.7976931348623157E308(inclusive). (4.9E-324 is 4.9 * 10^-324.)

The primary limitation for Java double values is precision. See Floating point roundoff error.

Converting from int to double

int a = 5;
double ad = a;
System.out.println(a);        // prints 5
System.out.println(ad);       // prints 5.0
System.out.println(a + 5.6);  // prints 10.6

Every value that can be represented as an int can be represented as a double. Many conversions from int to double happen automatically.

The statement double ad = a; sets the value of the new variable ad to a copy of the value of the variable a. The value of the variable a is 5 (an int). The value 5 is converted to 5.0 (a double) before it is stored in ad. The type of a remains int and the value of a remains 5.

In the expression a + 5.6, the value 5 is automatically converted to 5.0 prior to the addition. Again, the value of a remains 5.

Casting from int to double

int d = 25, e = 2;
System.out.println(d / e);           // prints 12
System.out.println(d / (double) e);  // prints 12.5

Java automatically converts from int to double only when the conversion is required. If the requested operation can be performed using an int, the value remains an int.

A cast can be used to explicitly specify that a value is to be converted. In the example above, a copy of the value of e is converted to a double prior to the division. See Division operations for additional discussion.

Converting from double to int

double b = 7.8;
int bi = (int) b;
System.out.println(b);  // prints 7.8
System.out.println(bi); // prints 7

There are many values that can be represented as double that cannot be represented as int. Converting from double to int requires a cast, as shown in the above example. The cast acknowledges the loss of information. In the example above, the decimal portion of 7.8 is lost.

The range of values that can be represented as double significantly exceeds the range that can be represented as int. Crae should be taken when casting values that may be outside the int range to int.

Java float type

float c = 9.6f;
double cd = c;
System.out.println(c);  // prints 9.6
System.out.println(cd); // prints 9.600000381469727

Java offers a float type that represents decimal numbers less precisely than the double type. Unless otherwise specified, Java assumes that decimal literals are of type double. As shown above, literals can be explicitly specified as float type by appending f to the literal value.

As shown above, conversions between float and double may result in roundoff error.

Help & comments

Get help from AP CS Tutor Brandon Horn

Comment on Numeric types