Advertisement
Constructor | Description |
---|---|
Double(double d) | Constructor of Double wrapper class takes a primitive double value. |
Double(String str) | Constructor of Double wrapper class also takes a String equivalent of a primitive double value.. |
class A
{
public static void main(String... ar)
{
Double d1 = new Double(10.5); //Boxing primitive double in Double object.
Double d2 = new Double("20.8"); //Passing primitive double as a String.
Double d3 = new Doube(10); //Passing primitive int 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());
}
}
10.5
20.9
11.0
Advertisement
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 |
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));
}
}
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
//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);
}
}
Primitive double value in d2 : 100.0
Primitive double value in d2 : 5.5
Primitive double value in d3 : -100.6
//Converting String to object of Double class by passing it to constructor of Double class.
class A
{
public static void main(String... ar)
{
Double b1 = new Double("100.5ft"); //Passing a String value that is not a valid primitive float value.
}
}
Exception in thread "main" java.lang.NumberFormatException: For input string: "100ft"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at java.lang.Double.(Unknown Source)
at A.main(Ex6.java:7)
//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.
}
}
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)
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);
}
}
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
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement