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

double vs. Double

So, you’ve mastered the basics of using double for floating-point arithmetic in Java. Great job! But have you ever wondered why there’s both a primitive type named double and a wrapper class called Double? Let’s unravel that mystery and see when you might pick one over the other.

Differences Between Primitives and Wrapper Classes

Java provides primitive data types like double for efficiency and simplicity. They aren’t objects, so they don’t come with object-oriented features. On the flip side, wrapper classes like Double wrap those primitive values in an object. That means you can store them in data structures that only accept objects, such as Collections. For example, a List<Double> can hold many doubles, but it can’t hold the primitive double.

In short, a double is a raw numeric value stored in a fixed amount of memory. A Double is an object that also stores a numeric value, but with extra overhead for things like methods (think doubleValue() or isNaN()). This overhead includes more memory usage and slower performance in certain contexts. If you just want to do arithmetic at lightning speed, double is your friend. If you need an object that can be null or placed in a collection, Double is the way to go.

When to Use double vs. Double

If you’re writing numerical computations in a tight loop, doing a lot of math, or optimizing for raw speed and minimal memory usage, double is typically better. Primitives avoid the overhead of object creation and garbage collection. That can mean a big difference if you’re running performance-sensitive code.

On the other hand, Double is essential if you need to store your numeric values in a data structure like an ArrayList<Double>, or if you want to set them to null to indicate the absence of a value (primitives can’t be null). This can also come in handy when calling methods that specifically require an object type. So if your code is heavily object-oriented or you need the optional null state, Double might be your go-to choice.

The bottom line is that there’s no single right answer. If you just need a numeric value that gets tossed around quickly, double is typically more efficient. If you need object-oriented features or null states, Double steps in. It all depends on your context and the trade-offs you’re willing to accept.