Advertisement



< Prev
Next >



Double Wrapper Class





Double wrapper class is used to create an object version of a primitive double value.





class A
{
public static void main(String... ar)
{
Double d1 = new Double(10.5); //Boxing primitive double in a  Double object.

System.out.println(d1);	//Unboxing a Double object.


//Reading primitive double out of Double object using doubleValue() method
Double d2 = new Double("20.9"); //Passing primitive double as a String.
System.out.println(d2.doubleValue());


Double d3 = new Double("11"); //Passing primitive int value.
System.out.println(d3.doubleValue());


}
}


Output-


10.5
20.9
11.0



Advertisement




Some important methods of Double wrapper class


Methods Description
boolean doubleValue() Returns a primitive double value out of Double wrapper object.
int compareTo(Double b) Returns a zero if the invoked Double object contains the same Double value as b. Returns a positive value if the invoked Double object contains greater value than b. Returns a negative value if invoked object contains smaller value than b.
boolean equals(Object ob) Returns a true if invoking Double object has same value as referred by ob, else false.
static Double parseDouble(String s) Returns a primitive double value converted from a String value.
static Double valueOf(double b) Returns a Double object after converting it from primitive Double value, b.
static Double valueOf(String s) Returns a Double object after converting it from a String, s.
short shortValue() Converts Double object to a primitive short value and returns it.
byte byteValue() Converts Double object to a primitive byte value and returns it.
int intValue() Converts Double object to a primitive int value and returns it
long longValue() Converts Double object to a primitive long value and returns it.
float floatValue() Converts Double object to a primitive float value and returns it
double doubleValue() Converts Double object to a primitive double value and returns it





Using compareTo() method to compare values in two Double objects.


Method compareTo(Double d) takes Double class object and it -
class A
{
public static void main(String... ar)
{
Double d1 = new Double("5.5"); //Constructor accepting String value
Double d2 = new Double(5.5);	  //Constructor accepting primitive boolean value

System.out.println("Value in d1 = "+d1);
System.out.println("Value in d2 = "+d2);

System.out.println("Invoking d1 to compare with d2 : "+ d1.compareTo(d2));

Double d3 = new Double("20.5");
Double d4 = new Double(10.5);

System.out.println("Value in d3 = "+d3);
System.out.println("Value in d4 = "+d4);

System.out.println("Invoking d3 to compare with d4 : "+ d3.compareTo(d4));

System.out.println("Invoking d4 to compare with d3 : "+ d4.compareTo(d3));

}
}

Output-

Value in d1 = 5.5
Value in d2 = 5.5
Invoking d1 to compare with d2 : 0
Value in d3 = 20.5
Value in d4 = 10.5
Invoking d3 to compare with d4 : 1
Invoking d4 to compare with d3 : -1





parseBoolean() method of Double class


Method parseDouble(String s), takes a string and parse it to a primitive double value.

//Wrapper class Double

import java.util.*;

class A
{
public static void main(String... ar)
{
double d1 = Double.parseDouble("100");
double d2 = Double.parseDouble("5.5");
double d3 = Double.parseDouble("-100.6");

System.out.println("Primitive double value in d2 : "+ d1);
System.out.println("Primitive double value in d2 : "+ d2);
System.out.println("Primitive double value in d3 : "+ d3);
}
}


Output-


Primitive double value in d2 : 100.0
Primitive double value in d2 : 5.5
Primitive double value in d3 : -100.6





Exception when converting a String to a Double object/primitive double.


NumberFormatException is raised when we pass String value to the constructor of Double class or its method parseDouble(), which is not a legal double value or it is out-of-range.

//Converting String to primitive double value by passing it to  parseDouble() method of Integer class.

class A
{
public static void main(String... ar)
{
double d = Double.parseDouble("100df"); //Passing a String value that can't be converted to an double.
}
}


Output -


Exception in thread "main" java.lang.NumberFormatException: For input string: "100df"
        at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
        at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
        at java.lang.Double.parseDouble(Unknown Source)
        at A.main(Double6.java:7)





Converting the value stored in a Byte object to all numeric primitive types.


import java.util.*;

class A
{
public static void main(String... ar)
{
Double d = new Double(21.0);	//Boxing a floating value to a Double object

System.out.println("Value in wrapped object, d : "+ d);

byte b = d.byteValue();		//Returns a primitive byte value out of a wrapped Double object
short s= d.shortValue();	//Returns a primitive short value out of a wrapped Double object
int i = d.intValue();		//Returns a primitive int value out of a wrapped Double object
long l = d.longValue();		//Returns a primitive long value out of a wrapped Double object
float f = d.floatValue();	//Returns a primitive float value out of a wrapped Double object
double d = d.doubleValue();	//Returns a primitive double value out of a wrapped Double object

System.out.println("byte value of Double object, d : " + b);
System.out.println("short value of Double object, d : " + s);
System.out.println("int value of Double object, d : " + i);
System.out.println("long value of Double object, d : " + l);
System.out.println("float value of Double object, d : " + f);
System.out.println("double value of Double object, d : "+ d);

}
}


Output-


Value in wrapped Double object, d : 21.0
byte value of Double object, d : 21
short value of Double object, d : 21
int value of Double object, d : 21
long value of Double object, d : 21
float value of Double object, d : 21.0
double value of Double object, d : 21.0




Please share this article -





< Prev
Next >
< Float Wrapper Class
Autoboxing and Unboxing>



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement