Java Double – Tutorial With Programming Examples- With the help of terminology and programming examples, we’ll look at the double data type in this article.

The Java decimal format and huge decimal classes are presented here, along with some commonly asked questions to assist you in fully comprehending the double data type.

## Java Primitive Types

Int, short, long, byte, float, double, char, and boolean are the eight primitive types of Java. The Java double is a primitive data type with a larger width and range than float.

## Java Double

Java double is used to represent floating-point numbers. It uses 64 bits to store a variable value and has a range greater than float type.

**Syntax:**

// square root variable is declared with a double type. double sqrt;

## Java Double Example

In this case, we’re calculating the square root of a rectangle’s area. We determined the area as an integer by taking the length and breadth as integers.

We declared the variable Area sqrt as double and calculated the square root because the square root is most likely to give you a decimal value.

`public` `class` `doubleExample {` ` ` `public` `static` `void` `main(String[] args) {` ` ` `int` `length=` `15` `, breadth=` `25` `;` ` ` `int` `area;` ` ` `area = length*breadth;` ` ` ` ` `// calculating area of the rectangle` ` ` `System.out.println(` `"Area of rectangle is "` `+ area);` ` ` ` ` `// declared a varibale which will store the square root` ` ` `double` `Area_sqrt;` ` ` ` ` `// calculating square root of Area of the rectangle` ` ` `Area_sqrt = Math.sqrt(area);` ` ` `System.out.println(` `"Square root of area is "` `+Area_sqrt);` ` ` ` ` `}` `}` |

**Output**

__Cyber Security Career__now!

## Java DecimalFormat

To format the integers, Java offers a particular class called DecimalFormat. This formatting can be changed.

We’ve established a pattern delimited by a comma ‘,’ and a decimal integer of type double in the example below. We will display our input number using this pattern or format.

We used the reference ‘df’ to format the result after passing the pattern into the Decimal format class.

`import` `java.text.DecimalFormat;` `public` `class` `ExampleFormat {` ` ` `public` `static` `void` `main(String[] args) {` ` ` ` ` `// defining a format in which number will be displayed` ` ` `String formatter = ` `"##,###,###.##"` `;` ` ` ` ` `// initialized the decimal number` ` ` `double` `num = ` `12345678.12` `;` ` ` ` ` `// passed the pattern into the Decimal format class` ` ` `DecimalFormat df = ` `new` `DecimalFormat(formatter);` ` ` ` ` `// printed the formatted number` ` ` `System.out.println(` `"The formatted number is: "` `+df.format(num));` ` ` ` ` `}` `}` |

**Output**

## Java BigDecimal

This is yet another specific Java class that performs basic arithmetic operations on a number (add, subtract, multiply, and divide), as well as rounding off the result, format conversion, and other functions.

To further grasp this, consider the example below.

Adding a decimal point

The difference between simple decimal subtraction and subtraction using the Big-Decimal class is exemplified in the example below.

We have determined the difference between the values of two double variables that have been initialised. We determined the difference between two variables that were both initialised with the same value using the Big-Decimal class.

Finally, we printed both figures so that you could see the difference. Big Decimal’s determined value was automatically rounded off.

`import` `java.math.BigDecimal;` `public` `class` `example {` ` ` `public` `static` `void` `main(String[] args) {` ` ` ` ` `// Initialized two double numbers` ` ` ` ` `double` `length1 = ` `1.06` `;` ` ` `double` `breadth1 = ` `1.07` `;` ` ` ` ` `// Subtracting length and breadth` ` ` `double` `sub = breadth1-length1;` ` ` `System.out.println(` `"Simple Subtraction = "` `+sub);` ` ` ` ` `// Initialized two big decimal numbers with same value` ` ` `BigDecimal length2 = ` `new` `BigDecimal(` `"1.06"` `);` ` ` `BigDecimal breadth2 = ` `new` `BigDecimal(` `"1.07"` `); ` `// Subtracting length and breadth` ` ` `length2 = breadth2.subtract(length2); ` ` ` `System.out.println(` `"Big Decimal Subtraction = "` `+ length2); ` ` ` ` ` `}` `}` |

**Output**

## Conclusion

We’ve covered the double primitive type with an acceptable example in this tutorial. With the applications, we’ve included DecimalFormat and BigDecimal Java.

In addition, frequently asked questions are contained in various areas of the double type, such as range, breadth, size, Math class, and so on.

After completing this tutorial, you will have a thorough understanding of the double type and will be able to use these concepts to developing your own arithmetic reasoning.

## Frequently Asked Questions

Q #1) How many bytes does a double type take?

8 bytes

Q #2) What is MathContext in Java?

The MathContext is a Java class that specifies the number rounding mode and accuracy. It provides immutable objects and is also responsible for enforcing certain requirements for the Big Decimal class’s operators.

- RoundingMode.CEILING,
- RoundingMode.DOWN,
- RoundingMode.FLOOR,
- RoundingMode.UP

In the example below, we’ve established different rules for rounding digits and initialised a double variable. This is in agreement with the output specifier we’ve provided.

For instance, in the first print statement, we’re calculating the ceiling function using the output specifier ‘3’. The output will have three digits as a result of this. Similarly, we passed ‘1’ in the last statement, indicating that the output will only contain one digit.

`import` `java.math.BigDecimal;` `import` `java.math.MathContext;` `import` `java.math.RoundingMode;` `public` `class` `example {` ` ` `public` `static` `void` `main(String[] args) {` ` ` ` ` `double` `d = ` `3.14` `; ` ` ` `// Rounded off to the upper limit, the output will contain 3 digit` ` ` `System.out.println(` `new` `BigDecimal(d, ` `new` `MathContext(` `3` `,` ` ` `RoundingMode.CEILING)));` ` ` ` ` `// Rounded off to the lower limit, the output will contain 3 digit` ` ` `System.out.println(` `new` `BigDecimal(d, ` `new` `MathContext(` `3` `,` ` ` `RoundingMode.DOWN)));` ` ` ` ` `/*` ` ` `* Rounded off to the previous integer (discards the decimal` ` ` `value)` ` ` `* The output will contain 1 digit` ` ` `*/` ` ` `System.out.println(` `new` `BigDecimal(d, ` `new` `MathContext(` `1` `,` ` ` `RoundingMode.FLOOR)));` ` ` ` ` `/*` ` ` `* Rounded off to the next integer (discards the decimal and ` `increments integer)` ` ` `* The output will contain 1 digit` ` ` `*/` ` ` `System.out.println(` `new` `BigDecimal(d, ` `new` `MathContext(` `1` `,` ` ` `RoundingMode.UP)));` ` ` `}` `}` |

**Output**

Q #3) Is Java Big Decimal immutable?

Yes, it is correct. When we do a specific operation in Big Decimal, they always return a new object rather than changing the already existing objects.

Q #4) What is a Math class?

In Java, a Math class contains all of the methods that are used in mathematical operations. E (2.72) and pi are two double constants (3.14).

Trigonometry methods such as sin(), cos(), and tan() are examples. exponential techniques sqrt(), log(), and pow() An example of using pow() in programming has already been provided (Java double example).