Signup/Sign In

Java Double Class

The Double class is a wrapper class that is used to wrap a value of the primitive double type in an object. An object of type Double contains a single field whose type is double. The Double class extends Number class and implements Comparable interface.

In addition, this class provides several methods for converting a double to a String and a String to a double, as well as other constants and methods useful when dealing with a double. Declaration of the class is given below.

Declaration:

public final class Double extends Number implements Comparable<Double>

Here we are explaining the methods of Double class and their example.

1. toString()

It returns a new String representing of specified double object. Syntax of the method is given below.

Syntax:

	
public String toString(double b)
	

Example:

Lets take an example to get string object of a double type. We used toString() method which is static so we can call it by using the class name. See the below example.

	
public class DoubleDemo1
{ 
    public static void main(String[] args) 
    { 
        double a = 46.23; 
		System.out.println("toString(a) = " + Double.toString(a)); 
     }
}
	

double-class

2. valueOf()

This method returns a Double instance representing the specified double value. This method should generally be used in preference to the constructor Double(double). It takes a single argument of double type.

Syntax:

	
public static Double valueOf(double b)
	

Example:

In this example, we are using valueOf() method that returns instance of Double class which represents the specified double type.

	
public class DoubleDemo1
{ 
    public static void main(String[] args) 
    { 
        double a = 46.23; 
        String b = "85";
        Double x = Double.valueOf(a); 
	   	System.out.println("valueOf(a) = " + x); 
        x = Double.valueOf(b); 
		System.out.println("ValueOf(b) = " + x); 
     }
}
	

double-class-example

3. parseDouble()

This method returns a double value of the specified string value. We can use it to get a double value from string type value. It takes a single String type argument.

Syntax:

	
public static double parseDouble(String val) throws NumberFormatException
	

Example:

Lets take an example in which we have a string type variable and getting its double value using the parseDouble() method.

	
public class DoubleDemo1
{ 
    public static void main(String[] args) 
    { 
        double a = 46.23; 
	String b = "85";
        double x = Double.parseDouble(b); 
System.out.println("parseDouble(b) = " + x);
     }
}
	

double-class-example

4. byteValue()

This method is used to get a primitive type double value from Double object. It returns the numeric value represented by this object after conversion to type double.

Syntax

	
public byte byteValue()
	

5. shortValue()

This method returns the value of this Double as a double after a widening primitive conversion. Syntax of this method is given below.

Syntax

	
public short shortValue()
	

6. intValue()

The intValue() method returns the value of this Double as a primitive int type after a widening primitive conversion. Syntax of this method is given below.

Syntax

	
public int intValue()
	

7. longValue()

The longValue() method returns the value of this Double type as a long type after a widening primitive conversion. Syntax of this method is given below.

Syntax

	
public long longValue()
	

8. doubleValue()

It returns the value of this Double type as a double type after a widening primitive conversion. Syntax of this method is given below.

Syntax

	
public double doubleValue()
	

9. floatValue()

This method is used to get value of this Double type as a float type after a widening primitive conversion. Syntax of this method is given below.

Syntax

	
public float floatValue()
	

Example:

Lets take an example to convert double type to int, long and float type values. In this example, we are using intValue(), floatValue(), doubleValue() methods.

	
public class DoubleDemo1
{ 
    public static void main(String[] args) 
    { 
        double a = 46.23; 
		Double obj = new Double(a); 
		System.out.println("bytevalue(obj) = " + obj.byteValue()); 
		System.out.println("shortvalue(obj) = " + obj.shortValue()); 
		System.out.println("intvalue(obj) = " + obj.intValue()); 
		System.out.println("longvalue(obj) = " + obj.longValue()); 
		System.out.println("doublevalue(obj) = " + obj.doubleValue()); 
		System.out.println("floatvalue(obj) = " + obj.floatValue());
     }
}
	

double-class-example

10. hashCode()

This method is used to get hash code of a double type value. It returns an int value of double object.

Syntax:

	
public inthashCode()
	

Example:

	
public static void main(String[] args) 
    { 
        double a = 46.23; 
        Double obj = new Double(a); 
        int x = obj.hashCode(); 
   	  	System.out.println("hashcode(x) = " + x); 
     }
}
	

double-class-example

11. isNaN()

This method returns a boolean value either true or false. It returns true, if this Double value is a Not-a-Number (NaN), false otherwise.

Syntax:

	
public booleanisNaN()
	

Example:

Lets take an example to check whether the given double value is NaN or not. See the below example.

	
public class DoubleDemo1
{ 
    public static void main(String[] args) 
    { 
        double a = 46.23; 
        Double obj = new Double(a); 
        Double x = Double.valueOf(a); 
    	System.out.println("isNaN(x) = " + x.isNaN());  
     }
}
	

double-class-example

12. isInfinite()

This method is used to check whether the double value is infinitely large in magnitude. It returns a boolean value either true or false. Syntax of this method is given below.

Syntax:

	
public booleanisInfinite()
	

Example:

We can use this method to check the range of Double value whether it lies under infinitely or not.

	
public class DoubleDemo1
{ 
    public static void main(String[] args) 
    { 
        double a = 46.23; 
        Double obj = new Double(a); 
        Double x = Double.valueOf(a);
        x = Double.valueOf(Double.POSITIVE_INFINITY + 1); 
   	    System.out.println("Double.isInfinite(x) = " + Double.isInfinite(x.doubleValue()));   
     }
}

	

double-class-example

13. toHexString()

This method is used to get a hexadecimal string representation of the double argument. It takes a double type argument that would be convert into hexadecimal value. Syntax of this method is given below.

Syntax:

	
public static String toHexString(double val)
	

Example:

	
public class DoubleDemo1
{ 
    public static void main(String[] args) 
    { 
        double a = 46.23; 
        Double obj = new Double(a); 
        Double x = Double.valueOf(a);
	    System.out.println("Double.toHexString(a) = " + Double.toHexString(a));         
    }
}
	

double-class-example

14. doubleToLongBits()

This method is used to get representation of the specified floating-point value according to the IEEE 754 floating-point "single format" bit layout. It takes a floating-point argument. Syntax of this method is given below.

Syntax:

	
public static long doubleToLongBits(double val)
	

Example:

In this example, we are using floattointbits() method that returns a bit layout of floating-point value.

	
public class DoubleDemo1
{ 
    public static void main(String[] args) 
    { 
        double a = 46.23; 
		Double obj = new Double(a); 
		long x = Double.doubleToLongBits(a); 
		System.out.println("Double.doubleToLongBits(a) = " + x);        
	}
}
	

double-class-example

15. doubleToRawLongBits()

This method returns a representation of the specified floating-point value according to the IEEE 754 floating-point "single format" bit layout, preserving Not-a-Number (NaN) values. Syntax of this method is given below.

Syntax:

	
public static long doubleToRawLongBits(double val)
	

Example:

	
public class DoubleDemo1
{ 
    public static void main(String[] args) 
    { 
        double a = 46.23; 
		Double obj = new Double(a); 
		long x = Double.doubleToRawLongBits(a); 
		System.out.println("Double.doubleToRawLongBits(a) = " + x);        
	}
}
	

double-class-example

16. LongBitsToDouble()

This method is used to get the double floating-point value with the same bit pattern. The argument is considered to be a representation of a floating-point value according to the IEEE 754 floating-point "single format" bit layout. Syntax of this method is given below.

Syntax

	
public static double LongBitsToDouble(long b)
	

Example:

Lets take an example to understand the intbitstofloat() method that returns floating-point value.

	
public class DoubleDemo1
{ 
    public static void main(String[] args) 
    { 
        double a = 46.23; 
		Double obj = new Double(a); 
		long x = Double.doubleToLongBits(a); 
        double y = Double.longBitsToDouble(x); 
		System.out.println("Double.LongBitsToDouble(x) = " + y);       
	}
}
	

double-class-example

17. equals()

The equals() method compares an object to the specified object. It returns true if objects are same; false otherwise. Syntax of this method is given below.

Syntax:

	
public boolean equals(Object obj)
	

Example:

We are comparing two double objects using the equals method that returns true if both the objects are true.

	
public class DoubleDemo1
{ 
    public static void main(String[] args) 
    { 
        double a = 46.23; 
		String b ="12";
		Double obj = new Double(a);
		Double obj1 = new Double(b); 
		boolean x = obj.equals(obj1); 
		System.out.println("obj.equals(obj1) = " + x); 
	}
}
	

double-class-example

18. compareTo()

This method is used to compare two double objects numerically. It returns 0, if the both double objects are equal. It returns less the 0, if one double object is less than argument object. It returns greater than 0, if one double object is numerically greater than the argument double object. Syntax of this method is given below.

Syntax:

	
public intcompareTo(Double b)
	

Example:

In this example, we are comparing two double objects using compareTo() method that compares two double objects numerically and returns a numeric value.

	
public class DoubleDemo1
{ 
    public static void main(String[] args) 
    { 
        double a = 46.23; 
		String b ="12";
		Double obj = new Double(a);
		Double obj1 = new Double(b); 
		int x = obj.compareTo(obj1); 
		System.out.println("obj.compareTo(obj1) = " + x); 
	}
}
	

double-class-example

19. compare()

It is used to compare two double values numerically. The value returned is identical to what would be returned by. Syntax of this method is given below.

Syntax:

	
public static int compare(double x,double y)
	

Example:

We can use compare method to compare two double values. It returns 0 if both are equal else returns either negative or positive numerical value.

	
public class DoubleDemo1
{ 
    public static void main(String[] args) 
    { 
        double a = 46.23; 
		String b ="12";
		Double obj = new Double(a);
		Double obj1 = new Double(b); 
		int x = Double.compare(obj, obj1); 
		System.out.println("compare(obj, obj1) = " + x); 
	}
}
	

double-class-example