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:
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:
public class DoubleDemo {
public static void main(String[] args) {
double myDouble = 7/2;
System.out.println(myDouble);
}
}
The result is:
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:
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.
GO TO FULL VERSION