CodeGym /Java Blog /Java Numbers /Java double keyword
Author
Alex Vypirailenko
Java Developer at Toshiba Global Commerce Solutions

# Java double keyword

The Java double keyword, as in any other language, indicates a computer format for representing a floating point number, occupying 64 bits, or 8 bytes, in memory. In this article we are going to talk about double and look at some examples.

## Floating point and computing: a short mathematical description

Fractional numbers are either fixed or floating point. The first option can be represented as a regular fraction, where the numerator (the number itself) and the denominator (its scaling factor) will be integers. For example, the number 2.7 is 27 with a scaling factor of 10, 3.14 - 314 with a factor of 100. However, this approach is not very accurate from a computational point of view, therefore, they often use floating point representation. In computing, floating-point arithmetic is a special arithmetic representation of real numbers as an approximation to support a trade-off between range and precision. The main format for representing floating point numbers in Java is called float. Its name comes from floating point. Float is 32 bits, of which 1 bit is signed bit, 8 bit for exponent and 23 bit for significand. It's range is ±3.40282347E + 38F i.e. 6-7 significant digits. The name double comes from double float. The double type is twice as large as float: 8 bytes versus 4. It is also called a double precision real number. Of the 64 bits reserved for a double number, 1 is signed bit, 11 bits are for exponent and 52 bits are for significand. Java double fraction stores numbers in range ±1.79769313486231570E + 308 i.e. 15-16 significant digits. Double is a more precise format. So if you need to store really large numbers preferring double over float is a good idea. By the way, mathematical methods such as sqrt, sin or cos and many others return double values. However, you should pay for double precision with memory.

## Creating a double variable

The double type is used to store real numbers. To create a variable in the code that will be able to store real numbers, you need to use the command:
``````
double name;
``````
Where name is the name of the variable.
``````
double myPrice;  //here we create a variable called myPrice
double action; //and here -- action.
``````
You can also use shorthand to create multiple variables of type double:
``````
double name1, name2, name3;
``````

## Java double keyword Examples

Let's give some examples of using the Java double keyword to create a variable.
``````
double myPrice = 5.0;
double height = 180;
double x = 7.1, y = 3.0;
``````
Here in the variable myPrice we have the value 5.0, in the variable height — 180, in x we put the value 7.1, and 3.0 in y.

### Double as an integer

In Java, double variables can be assigned both real and integer numbers. When assigning integers, they are simply converted to real numbers. Although sometimes a slight loss of accuracy is possible.
``````
double height = 180;
int k = 2;
int i = 5;
double myDouble = k*i;``````
In fact, the height variable stores the number 180.0, and the myDouble variable stores the number 10.0.

### Double and integer interaction

In addition, if an integer and a real number are involved in some expression, then the integer is first converted to a real number and only then interacts with another real number.
``````
public class DoubleDemo {
public static void main(String[] args) {
int k = 2;
double myDouble1 = 5;
double myDouble = k*7.0;
System.out.println(myDouble1);
System.out.println(k*myDouble1);
System.out.println(myDouble);
}
}
``````
In this example, the output will be:
5.0 10.0 14.0
Even though the number myDouble1 is denoted as 5 and not 5.0, Java sees this number as a double, so it actually looks like 5.0. If we multiply int and double, we always get a double, even if in fact this number is an integer. We can assign variables of type double to variables of type int. To do this, you need to make an explicit type conversion. Of course, the fractional part will be discarded, the number will be truncated to a smaller integer.
``````
public class DoubleDemo {
public static void main(String[] args) {
double x = 57.789;
int almostX;
almostX = (int)x;
System.out.println(almostX);
}
}
``````
The output is:
57
Finally, let's talk about division. This is the most interesting thing. You may have already come across the fact that if you divide two integers, then as a result of division we get an integer, even if they are not evenly divisible by each other:
``````
public class DoubleDemo {
public static void main(String[] args) {
double myDouble = 7/2;
System.out.println(myDouble);
}
}
``````
The result is:
3.0
This is because the Java machine first divides two integers (and gets 3), and then stores this value in a variable of type double, and gets 3.0 as a result. To get not integer, but usual division, you need to cheat. For example, write one of the numbers as a real number (then the whole expression is automatically converted to real). If we work with variables of integer type, then they can be multiplied by 1.0. This will not change the value, but it will change the type of the variable from int to double.
``````
public class DoubleDemo {
public static void main(String[] args) {
double myDouble = 7.0/2;
int x = 5;
int y = 2;
System.out.println(myDouble);
System.out.println(x*1.0/y);
}
}
``````
The output is:
3.5 2.5
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION