HudaTutorials.com

java lang Double Class - java.lang.Double Class in Java

Last updated on

java.lang.Double Class

Double Class in Java

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.

Java Double class provides several useful methods for converting a double to a String and a String to a double, as well as other constants and methods.

What is Java Double Class ?

Double class is a wrapper class for the primitive type double which contains several methods to effectively deal with a double value.

java.lang.Double Class Example

/*	Java Double Class Example
	Save with file name DoubleExample.java	*/
public class DoubleExample
{
	public static void main(String args[])
	{
		// JAVA DOUBLE DECLARATION
		Double b;
		// MEMORY ALLOCATION FOR JAVA DOUBLE
		b = new Double(101);
		// JAVA DOUBLE CLASS OUTPUT
		System.out.println("Java Double Class Example");
		// 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

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

java.lang.Double Class Example 3

Following Java Double class example you can learn how to use static methods of Java 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 Double Class Example 3
	Save with file name DoubleExample3.java	*/
public class DoubleExample3
{
	public static void main(String args[])
	{
		// JAVA DOUBLE CLASS STATIC METHODS USAGE
		System.out.println("Java Double Class Example");
		// RETURN Java Double OBJECT WITH THE
		// SPECIFIED PRIMITIVE double DATA TYPE
		System.out.println("valueOf : "+ Double.valueOf(101));
		// RETURN Java 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"));
	}
}