Menu
Topics Index
...
`


Exploring java.lang > Primitive Type Wrappers >
Siva Nookala - 05 Apr 2016
As Java is an Object Oriented language sometimes we may face some situations where we have to use objects instead of primitive data types, so Java provides wrapper classes for every primitive data type. In this topics we will see how to create objects for Byte, Short, Integer, Long and also their methods.

Creating Objects for primitive data types:
The following are the constructors used in creating objects for primitive data type.
For Byte :
Byte(byte num)
Byte(String str) throws NumberFormatException

For Short :
Short(short num)
Short(String str) throws NumberFormatException

For Long :
Long(long numb)
Long(String str) throws NumberFormtatException

The above shown are the constructors used to construct the objects from numeric values or from strings that contain valid whole number values.
The below program shows how they function.
ConstructorsDemo
class ConstructorsDemo
{
    public static void main(String[] args)
    {
        Byte b = new Byte((byte) 5); // LINE A
        Byte b_str = new Byte("5" + 1);
        System.out.println("Value of b : " + b + " Value of b_str " + b_str);
        Short s = new Short((short) 10); // LINE B
        Short s_str = new Short("10" + 2);
        System.out.println("Value of s : " + s + " Value of b_str " + s_str);
        Integer i = new Integer(15); // LINE C
        Integer i_str = new Integer("15" + 3);
        System.out.println("Value of i : " + i + " Value of b_str " + i_str);
        Long l = new Long(20); // LINE D
        Long l_str = new Long("20" + 4);
        System.out.println("Value of i : " + l + " Value of b_str " + l_str);
    }
}
OUTPUT

Value of b : 5 Value of b_str 51
Value of s : 10 Value of b_str 102
Value of i : 15 Value of b_str 153
Value of i : 20 Value of b_str 204

DESCRIPTION

In the above program we have created objects for all the wrapper classes using the constructors. At LINE A we have created numeric and String objects for Byte class. In the same way we have created numeric and String objects for Short, Integer, Long classes at LINE B, LINE C, LINE D.

THINGS TO TRY
  • Remove the type casting byte at LINE A to see a compilation error because by default java considers all whole numbers as integers.
  • Replace the line after LINE A with the below code.
    Byte b_str = new Byte("5" + 1 + 2);
    A run time error Exception in thread "main" java.lang.NumberFormatException: Value out of range. Value:"512" Radix:10 is thrown, since the maximum value for byte is 127 and 512 is greater than 127.
The following are constants which can be used in the above wrapper classes.
Constant Description
MIN_VALUE Returns the minimum value that can be placed in datatype.
MAX_VALUE Returns the maximum value that can be placed in data type.
SIZE Returns the bit width of the wrapped value.
TYPE Returns the Class object for byte, short, int or long.
The above constants can also be used for all the other primitive wrapper classes.
Methods in Byte:
Method Description
byte byteValue() Returns the value of the invoking object as the byte.
static int compare(byte num1, byte num2) Compares the values of num1 and num2. Returns 0 if the values are equal. Returns a negative values if num1 is less than num2. Returns a positive value in num1 is greater than num2. (Added by JDK 7.)
int compareTo(Byte b) Compares th numerical value of the invoking object with that of b. Returns 0 if the values are equal. Returns a negative value if the invoking object has a lower value. Returns a positive value if the invoking object has a greater value.
double doubleValue() Returns the value of the invoking object as a double.
float folatValue() Returns the value of the invoking object as float.
int intValue() Returns the value of the invoking object as int
long longValue() Returs the value of the invoking object as long
short shortValue() Returs the value of the invoking object as short
equals(Object ByteObj) Returns true if the invoking Byte object is equivalent to ByteObj. Otherwise, it returns false.
static byte parseByte(String str, int radix) throws NumberFormatException Returns the byte equivalent of the number contained in the string specified by str using the specified radix.
String toString() Returns a string that contain the decimal equivalent of the invoking object.
String toString(byte num) Returns a string that contain the decimal equivalent of num.
static Byte valueOf(String str) throws NumberFormatException Returns Byte object containing the value passed in num.
static Byte decode(String str) throws NumberFormatException Returns a Byte object that contains the value specified by the string in str.
ByteDemo
class ByteDemo
{
    public static void main(String[] args)
    {
        Byte b = new Byte((byte) 2);
        System.out.println("Returns double value: " + b.doubleValue());
        System.out.println("Returns float value: " + b.floatValue());
        System.out.println("Returns long value " + b.longValue());
        System.out.println("Returns int value: " + b.intValue());
        Integer i = new Integer(2); // LINE A
        System.out.println("Compares the value: " + b.equals(i)); // LINE B
        System.out.println("Returns String object: " + b.toString());
        System.out.println("Byte equivalent String: " + Byte.toString((byte) 520));
        //520 - 256 = 264 -> 264 - 256 = 8 which is in byte range
        System.out.println("Decimal equivalent String: " + Byte.parseByte("25", 8)); // LINE C
        //Converts the value of given radix to a byte value.
        System.out.println("Returns Byte object: " + Byte.valueOf("32", 8)); // LINE D
    }
}
OUTPUT

Returns double value: 2.0
Returns float value: 2.0
Returns long value 2
Returns int value: 2
Compares the value: false
Returns String object: 2
Byte equivalent String: 8
Decimal equivalent String: 21
Returns Byte object: 26

DESCRIPTION

In the above program we have used the Byte methods which we are shown in the above table. At LINE B we are comparing the value of Byte object b with Integer object i which results true in the output, since both have same value. At LINE C we are converting the value 10110 of radix 2 to Byte value of radix 10. At LINE D we are converting a String value of given radix to Byte value which throws a NumberFormatException when passed parameter is not a numeric value.

THINGS TO TRY
  • At LINE A in the program change the value of the i to 10 due to which the statement at LINE B results false.
  • At LINE C in the program change the parameters 10110 and 2 to 25 and 8 which result in the output of 21. Here the value 25 of radix 8 is converted to the byte value of radix 10.
  • At LINE D change the value of 321 to Hai to see NumberFormatException.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App