java.lang.Double Class

The Double class wraps the value of primitive data type double into Double object. An object of type Double contains a single field whose type is double. In other words the wrapper classes create objects for primitive data types. The wrapper classes are Boolean, Byte, Character, Short, Integer, Float, Long and Double.

This class has following constructors.

Short Class Constructors

Double(double value)

Constructs a newly allocated Double object that represents the primitive double argument.

Double(String s)

Constructs a newly allocated Double object that represents the floating-point value of type double represented by the string.

java.lang.Double Class Example
/* Save with file name DoubleExample.java */

public class DoubleExample
{
	public static void main(String args[])
	{
		//DOUBLE DECLARATION
		Double b;
		//MEMORY ALLOCATION FOR DOUBLE
		b = new Double(101);
		//DOUBLE CLASS OUTPUT
		System.out.println();
		System.out.println("Double Class Example");
		System.out.println("=====================");
		System.out.println();
		//RETURNS byte PRIMITIVE DATA TYPE
		System.out.println("byte Value is : "+ b.byteValue());
		//RETURNS short PRIMITIVE DATA TYPE
		System.out.println("short Value is : "+ b.shortValue());
		//RETURNS double PRIMITIVE DATA TYPE
		System.out.println("double Value is : "+ b.doubleValue());
		//RETURNS float PRIMITIVE DATA TYPE
		System.out.println("float Value is : "+ b.floatValue());
		//RETURNS int PRIMITIVE DATA TYPE
		System.out.println("int Value is : "+ b.intValue());
		//RETURNS long PRIMITIVE DATA TYPE
		System.out.println("long Value is : "+ b.longValue());
	}
}
                        
java.lang.Double Class Example 2
/* Save with file name DoubleExample2.java */

public class DoubleExample2
{
	public static void main(String args[])
	{
		//DOUBLE DECLARATION
		Double b, b2;
		//MEMORY ALLOCATION FOR DOUBLE
		b = new Double(101);
		b2 = new Double("102");
		//DOUBLE CLASS OUTPUT
		System.out.println();
		System.out.println("Double Class Example");
		System.out.println("=====================");
		System.out.println();
		System.out.println("b Value is : "+ b.doubleValue());
		System.out.println("b2 Value is : "+ b2.doubleValue());
		System.out.println();
		System.out.println("b compareTo b2 : "+ b.compareTo(b2));
		System.out.println("b equals b2 : "+ b.equals(b2));
	}
}
                        

In the following example you can learn how to use static methods of Double Class. If you use static methods you need not to create the class instance. You can call static methods using the class name as reference.

java.lang.Double Class Example 3
/* Save with file name DoubleExample3.java */

public class DoubleExample3
{
	public static void main(String args[])
	{
		//DOUBLE CLASS STATIC METHODS USAGE
		System.out.println("Double Class Example");
		System.out.println("=====================");
		System.out.println();
		//RETURN Double OBJECT WITH THE SPECIFIED PRIMITIVE double DATA TYPE
		System.out.println("valueOf : "+ Double.valueOf(101));
		//RETURN Double OBJECT WITH THE SPECIFIED
		//PRIMITIVE double DATA TYPE AS String
		System.out.println("valueOf : "+ Double.valueOf("103"));
		//RETURN PRIMITIVE double DATA TYPE WITH THE SPECIFIED
		//PRIMITIVE double DATA TYPE AS String
		System.out.println("parseDouble : "+ Double.parseDouble("105"));
	}
}