This
Double
class has two constructors:
Double(double number):
This constructor takes
double
type value as parameter and converts it into
Double
class object.
Double(String string):
This constructor takes
String
type as parameter which contains double number and converts it into
b
class object.
Double
class contains following methods:
DoubleDemo
class DoubleDemo
{
public static void main(String arg[])
{
Double d = new Double(9.4);
/*double prim_d = d; // LINE A
System.out.println("Value converted to double: " + prim_d);*/
byte b = d.byteValue(); // LINE B
System.out.println("Value when converted to bye: " + b);
int i = d.intValue(); // LINE C
System.out.println("Value when converted to int: " + i);
short st = d.shortValue(); // LINE D
System.out.println("Value when converted to short: " + st);
long l = d.longValue(); // LINE E
System.out.println("Value when converted to long: " + l);
String s = d.toString(); // LINE F
System.out.println("Value when converted to String: " + s);
double d1 = Double.parseDouble(s); // LINE G
System.out.println("Converting String to double: " + d1);
}
}
OUTPUTValue when converted to bye: 9
Value when converted to int: 9
Value when converted to short: 9
Value when converted to long: 9
Value when converted to String: 9.4
Converting String to double: 9.4
DESCRIPTIONAt LINE B we have converted Double object d to primitive data type byte.
At LINE C we have converted Double object d to primitive data type int.
At LINE D we have converted Double object d to primitive data type short.
At LINE E we have converted Double object d to primitive data type long.
At LINE F we have converted Double object d to String s.
At LINE G we have converted String object s to primitive data type double.
Things to try
Things to try
THINGS TO TRY
- Replace the code at
LINE A
with below code.
byte b1 = (byte)d;
The above code line gives a compilation error since type casting cannot be performed on an object.
- Uncomment the code at
LINE A
and check the output. The output will be Value converted to double: 9.4. Since each time when a value is assigned to a primitive data type JVM
creates a wrapper class object for corresponding primitive data type.
Float:
Float
is a Primitive Wrapper Class. It is used to wrap the
float
value into
Float
object. This
Float
object is used to do further manipulations on the wrapped
float
value.
This
Float
class has three constructors:
Float(float number):
This constructor takes
float
type value as parameter and converts it into
Float
class object.
Float(double number):
This constructor takes
double
type value as parameter and converts it into
Float
class object.
Float(String string):
This constructor takes
String
type as parameter which contains float number and converts it into
Float
class object.
Methods in
Float
class:
Float and
Double define the following constants:
DoubleFloatDemo
import java.util.*;
class DoubleFloatDemo
{
public static void main(String arg[])
{
float f = 9.4f;
Float f_obj = new Float(f); // LINE A
//Compares the numeric values of two Float class objects
System.out.println("Values when f_obj compared with f: " + f_obj.compareTo(f)); // LINE B
//Compares the float object with any other object
System.out.println("f_obj and f are equal: " + f_obj.equals(f)); // LINE C
//Returns float equivalent of the string
System.out.println("Float equivalent of string: " + Float.parseFloat("10")); // LINE D
//Returns a string from a Float object
System.out.println("String representation of Float: " + Float.toString(f_obj)); // LINE E
}
}
OUTPUTValues when f_obj compared with f: 0
f_obj and f are equal: true
Float equivalent of string: 10.0
String representation of Float: 9.4
DESCRIPTIONAt LINE A
we have created a Float
object with the value of f.
At LINE B
we have compared f_obj
and f
with compareTo
which returned 0
since the values are same.
At LINE C
we have compared f_obj
and f
with equals
which returned true
.
At LINE D
we are converting a String
value to Float
object.
At LINE E
we are converting a Float
object to String
object.
THINGS TO TRY
- At
LINE A
instead of passing parameter f
pass a value 9.4
to see a compilation error since 9.4
is a double
value and the actual parameter to be passed is Float
object compiler throws a compilation error.
f_obj.compareTo(9.4));
Note: When we pass any primitive value JVM automatically converts it to its equivalent wrapper class object.
- At
LINE B
change the equals
method parameter to 9.4
and see the output. The output will become false since the values 9.4f
and 9.4
are not same.