The Wrapper Class: Abstract class Number, Double and Float

The package java.lang contains classes and interfaces that are fundamental to all of the Java programs and thus gets imported automatically into all the programs. One such group of classes is known as type wrappers. Wrapper classes are the class objects for our primitive datatypes like Integer for int, Float for float, Boolean for boolean etc.

Introduction to the Wrapper Class

In Java the basic data types such as int, float, etc. are treated as primitives or primitive data type and not as an object to enhance the performance of the program. Despite that, we might need to objectify these primitive data types to meet some goals like implementation in data structures or passing the values by reference and have some basic functionalities.

To solve this issue, Java introduces a set of classes known as wrapper classes or type wrappers which encapsulate the primitive data type in objects. These are the wrapper classes present in Java: Integer, Float, Long, Double, Short, Byte, Character, and Boolean. They contain methods that enable a programmer to easily merge the primitive data types with the object hierarchy of Java.


The Abstract “Number” Class

All the numeric data types, i.e. byte, short, int, long, float, and double are wrapped by the abstract class Number. This class is inherited by the classes that intend to wrap the above-stated data types. Number declares methods that return the value of the object in the following number formats:

  • byte byteValue(): This function returns the value of the object in byte.
  • short shortValue(): It returns the value as short.
  • int intValue(): It returns the value as int.
  • long longValue(): It returns the value as long.
  • float floatValue(): It returns the value as float.
  • double doubleValue(): It returns the value as double.

Note: All these data types do not have the same size so due to narrowing conversion the values might be truncated, rounded or can even result in garbage values.


Floating Point Type Wrappers

1) Float – The floating point values can either use the Float wrapper or the Double wrapper to wrap their values. Float objects can be constructed by using both float or double values and also by the string representation of any floating-point number. The constructors in the Float wrapper is given below:

Float(double num)
Float(float num)
Float(String str)throws NumberFormatException

2) Double – objects can be constructed either by double values or by the string representation of a floating-point number. The constructor in the Double wrapper is given below:

Double(double num)
Double(String str)throws NumberFormatException

An example containing the construction of a Float and Double object is shown below:

Float f = new Float(13.26);
Double d = new Double(152.337);

It is important to note that all the type wrappers override the method toString() which allows the wrapper object to output the values using the println() function.

Constants in Float and Double

The wrappers Float and Double define the following constants:

CONSTANTSDESCRIPTION
BYTESThe width of the float or double in bytes. This constant was added in JDK8.
MAX_EXPONENTMaximum Exponent
MAX_VALUEMaximum Positive Value
MIN_EXPONENTMinimum Exponent
MIN_NORMALMinimum Positive Normal Value
MIN_VALUEMinimum Positive Value
NaNNot a Number
POSITIVE_INFINITYPositive Infinity
NEGATIVE_INFINITYNegative Infinity
SIZEThe bit width of the wrapped class
TYPEThe Class Object for float or double

Some Methods defined in Float wrapper class

Here are some methods that are defined in the wrapper class Float:

METHODSDESCRIPTION
static int compare(float num1, float num2)Compares the value of num1 and num2 and returns 0 if values are equal. Returns a positive value if num1 is greater than num2 and negative value otherwise.
boolean equals(Object FloatObj)Returns true if the invoking Float object is equal to FloatObj otherwise false.
static int floatToIntBits(float num)Returns the IEEE-compatible, single precision bit pattern corresponding to num.
static int hashCode(float num)Returns the hash code for num. It was added by JDK8.
int hashCode()Returns the hash code for the invoking object.
static float intBitsToFloat(int num):Returns float equivalent of the IEEE-compatible, single precision bit pattern specified by num.
boolean isNaNReturns true if the invoking object contains a value that is not a number otherwise false.
boolean isInfinte()Returns true if the invoking object contains a value that is infinite otherwise false.
static boolean isFinite(float num)Returns true if num is not NaN or infinite otherwise false. It was added by JDK8.
static float min(float num1, float num2)Returns the minimum of the two numbers num1 and num2. It was added by JDK8.
static float max(float num1, float num2)Returns the maximum of the two numbers num1 and num2. It was added by JDK8.
static float sum(float num1, float num2)Returns the sum of the two numbers num1 and num2. It was added by JDK8.
static String toHexString(float num)Returns a string which contains the value of num in Hex format.
static String toString(float num)Returns the string equivalent of the value in num.
static Float valueOf(float num)Returns a Float object containing the value passed in num.
static Float valueOf(String str)throws NumberFormatExceptionReturns Float object that contains the value in str.

Some methods defined in Double wrapper class

METHODSDESCRIPTION
static int compare(double num1, double num2)Compares the value of num1 and num2 and returns 0 if values are equal. Returns a positive value if num1 is greater than num2 and negative value otherwise.
boolean equals(Object DoubleObj)Returns true if the invoking Double object is equal to DoubleObj otherwise false.
static int hashCode(double num)Returns the hash code for num. It was added by JDK8.
int hashCode()Returns the hash code for the invoking object.
static double longBitsToDouble(long num):Returns double equivalent of the IEEE-compatible, double precision bit pattern specified by num.
boolean isNaNReturns true if the invoking object contains a value that is not a number otherwise false.
boolean isInfinte()Returns true if the invoking object contains a value that is infinite otherwise false.
static boolean isFinite(double num)Returns true if num is not NaN or infinite otherwise false. It was added by JDK8.
static double min(double num1, double num2)Returns the minimum of the two numbers num1 and num2. It was added by JDK8.
static double max(double num1, double num2)Returns the maximum of the two numbers num1 and num2. It was added by JDK8.
static double sum(double num1, double num2)Returns the sum of the two numbers num1 and num2. It was added by JDK8.
static String toHexString(double num)Returns a string which contains the value of num in Hex format.
static String toString(double num)Returns the string equivalent of the value in num.
static Double valueOf(float num)Returns a Double object containing the value passed in num.
static Double valueOf(String str)throws NumberFormatExceptionReturns Double object that contains the value in str.
static double parseDouble(String str)throws NumberFormatExceptionReturns double equivalent of the number in str using radix 10.

Program to show use of Float and Double Wrapper Classes

/**
 * This program creates one float object and three double objects.
 * Check whether number is infinite or Nan and displays the sum of the last two numbers
 */

public class FloatDoubleDemo {

    public static void main(String args[]) {
        //Wrapping the float number into Float Object.infinite value
        Float f = new Float(1 / 0.);
        //NaN value
        Double d1 = new Double(0 / 0.);
        //wrapping the given double number into Double object
        Double d2 = new Double(52.46);
        //wrapping the given double number into Double object
        Double d3 = new Double(25.77);
        //calculating sum using the function sum
        double s = Double.sum(d2, d3);

		/*Displaying the values and using the function isInfinite() and isNan()*/
        System.out.println(f + ": " + f.isInfinite() + "," + f.isNaN());
        System.out.println(d1 + ": " + d1.isNaN());
        System.out.println("sum of " + d2 + " and " + d3 + " : " + s);
    }
}

Output:-
Infinity: true,false
NaN: true
sum of 52.46 and 25.77 : 78.23

So that’s all for this tutorial. Hope this helps and you like the tutorial. Do ask for any queries in the comment box and provide your valuable feedback. Do come back for more because learning paves way for a better understanding.

Keep Coding!! Happy Coding!! 🙂

Recommended -