Search⌘ K

Java Wrapper Class: Double

Explore the Java Double wrapper class to understand how it encapsulates primitive double values. Learn to create Double objects using different constructors, handle exceptions, and utilize important methods like doubleValue. Understand the significance of Double.MIN_VALUE and Double.MAX_VALUE for representing limits.

Introducing Double

The Double class is part of the java.lang package.

The Double class is a wrapper class for the primitive type double. It contains several methods to deal with double type values effectively. An object of Double class can hold a single double value.

Creating a Double object

This class has two constructors:

  • Double (double value)
  • Double (String string)

Let’s cover them one by one.

The Double (double value) constructor

This constructor constructs a new Double object that represents the specified double value.

Look at the program below.

Java
class DoubleWrapperDemo1
{
public static void main(String args[])
{
Double x = new Double(5); // Creating an Double type object
System.out.println(x);
}
}

Look at line 5. We create the Double object, called x. We pass 55 as the argument. Now, when we try to print x, we get 5.05.0 as a result. It behaves just like a standard double ...