Menu
Topics Index
...
`


Exploring java.lang > Primitive Type Wrappers >
Siva Nookala - 20 Feb 2017
Double:
Double class is a wrapper class which extends abstract Number class and is used to wrap a value of primitive data type double to an object.

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.

Doubleclass contains following methods:
Method Description
String toString() Returns a String representation for the specified Double object.
Double valueOf(String s) Returns the Double value represented by the specified String.
static boolean isNaN(double v) Returns true if the specified number is the special Not-a-Number value otherwise false.
boolean isNaN() Returns true if the Double object is the special Not-a-Number value otherwise false.
static boolean isInfinite(double v) Returns true if the specified number is infinitely large in magnitude.
boolean isInfinite() Returns true if this Double object is infinitely large in magnitude.
int intValue() Returns the integer value of this Double (by casting to an int)object.
long longValue() Returns the long value of this Double (by casting to a long).
float floatValue() Returns the float value of this Double object.
double doubleValue() Returns the double value of this Double.
int hashCode() Returns a hashcode for this Double.
boolean equals() Compares two objects and returns true if both are equal
long doubleToLongBits(double value) Returns the bit representation of the given value.
Double longBitsToDouble(long bits) Returns the double corresponding to a given bit representation.
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);    
    }
}
OUTPUT

Value 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

DESCRIPTION

At 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:
Method Description
String toString(float f) Returns a String representation for the specified float value.
Float valueOf(String s) Returns the floating point value represented by the specified String.
static boolean isNaN(float v) Returns true if the specified number is the special Not-a-Number value otherwise false.
boolean isNaN() Returns true if the float number is the special Not-a-Number value otherwise false.
static boolean isInfinite(float v) Returns true if the specified number is infinitely large in magnitude.
boolean isInfinite() Returns true if this float object is infinitely large in magnitude.
int intValue() Returns the integer value of this Float (by casting to an int).
long longValue() Returns the long value of this Float (by casting to a long).
float floatValue() Returns the float value of this Float object.
double doubleValue() Returns the double value of this Float.
int hashCode() Returns a hashcode for this Float.
boolean equals() Compares two objects and returns true if both are equal
int floatToIntBits(float value) Returns the bit representation of a single-float value.
float intBitsToFloat(int bits) Returns the single-float corresponding to a given bit representation.

Float and Double define the following constants:
MAX_EXPONENT Maximum exponent
MAX_VALUE Maximum positive value
MIN_EXPONENT Minimum exponent
MIN_NORMAL Minimum positive normal value
MIN_VALUE Minimum positive value
NaN Not a number
POSITIVE_INFINITY Positive infinity
NEGATIVE_INFINITY Negative infinity
SIZE The bit width of the wrapped value
TYPE The Class object for float or double
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    
    }
}
OUTPUT

Values 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

DESCRIPTION

At LINE A we have created a Float object with the value of f.
At LINE B we have compared f_obj and f with compareTowhich 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.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App