Fractional values of numbers with `double`
Use a double to store a number that has values after the decimal point.
The int
data type only represents numbers without a fractional part. What if we want to store an approximation for π in a mathematics program? We can use a different data type, called a double
, to store numbers that have values after the decimal point. double
is short for double-precision floating point. Floating point means that there is a decimal point that can be placed at different locations (or float), in the number.
Computer programming languages evolve over time; double-precision just means that this representation allows double the precision that an earlier representation of floating-point numbers used. There is also a floating point type called float
in Java, which uses less memory and has less precision than a double. It is rarely used.
Exercise: circle area
Objective: Create and use values and variables of the double
type.
Declare and give initial values to variables r
and pi
representing the radius of a circle, and an approximation of the mathematical constant π. Use those values to compute and print the area of the circle. You can square a number in Java by multiplying it by itself.
Create a free account to view this lesson.
By signing up, you agree to Educative's Terms of Service and Privacy Policy