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);
}
}
OUTPUTValue 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
DESCRIPTIONIn 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.
The following are constants which can be used in the above wrapper classes.
The above constants can also be used for all the other primitive wrapper classes.
Methods in Byte:
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
}
}
OUTPUTReturns 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
DESCRIPTIONIn 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.