JAVA Wrapper Class
In Java, Wrapper Class is used for converting primitive data type into object and object into a primitive data type. For each primitive data type, a pre-defined class is present which is known as Wrapper class. From J2SE 5.0 version the feature of autoboxing and unboxing is used for converting primitive data type into object and object into a primitive data type automatically.
Why Use Wrapper Classes?
As we knew that in Java when input is given by the user, it is in the form of String. To convert a string into different data types, Wrapper classes are used.
We can use wrapper class each time when want to convert primitive to object or vice versa.
The following are the Primitive Data types with their Name of wrapper class and the method used for the conversation.
Primitive DataType | Wrapper ClassName | Conversion methods |
---|---|---|
byte | byte | public static byte parseByte(String) |
short | Short | public static short parseShort(String) |
int | Integer | public static int parseInt(String) |
long | Long | public static long parseLong(String) |
float | Float | public static float parseFloat(String) |
double | Double | public static double parseDouble(String) |
char | Character | Â |
boolean | Boolean | public static boolean parseBoolean(String) |
In Java, all the primitive wrapper classes are immutable. When a new object is created the old object is not modified. Below is an example to demonstrate this concept.
Example:
Â
Number Class
Java Number class is the super class of all the numeric wrapper classes. There are 6 sub classes, you can get the idea by following image.
The Number class contains some methods to provide the common operations for all the sub classes.
Following are the methods of Number class with there example
1. Value() Method
This method is used for converting numeric object into a primitive data type. For example we can convert a Integer object to int or Double object to float type. The value() method is available for each primitive type and syntax are given below.
Syntax:
Example:
Here we are using several methods like: byteValue(), intValue(), floatValue()
etc for convert object type to primitive type. The double type object is used to fetch different types of primitive values.
Â
Java Integer class
Java Integer class is used to handle integer objects. It provides methods that can be used for conversion primitive to object and vice versa.
It wraps a value of the primitive type int in an object. This class provides several methods for converting an int to a String and a String to an int, as well as other constants and methods helpful while working with an int.
It is located into java.lang pakage and java.base module. Declaration of this class is given below.
Declaration
Methods of Integer class with examples are discussed below.
1. toString() Method
This method returns a String object representing this Integer’s value. The value is converted to signed decimal representation and returned as a string. It overrides toString()
method of Object class.
It does not take any argument but returns a string representation of the value of this object in base 10. Syntax of this method is given below.
Example:
In this example, we are using toString method to get string representation of Integer object.
Â
2. toHexString()
The toHexString()
method is used to get a string representation of the integer argument as an unsigned integer in base 16.
This method takes an argument of int type and returns a hexadecimal string. Syntax of the method is given below.
Syntax:
Example:
We are getting hexadecimal string value of an int value using the toHexString() method. In this example, we passed 95 as an argument and get 5f as a hexadecimal string. See the below example.
Â
3. toOctalString()
This method is helpful when we want to get an octal string representation of an int type value.
It can be used to get a string representation of the integer argument as an unsigned integer in base 8.
It takes an int type argument and returns an unsigned string representation of the argument. Syntax of this method is given below.
Syntax:
Example:
Lets take an example to get octal value of an int value. Here we are passing 95 to the toOctalString()
method and getting its octal value 135.
Â
4. toBinaryString()
The toBinaryString()
method is used to get a string representation of the integer argument as an unsigned integer in base 2.
It takes an integer argument and returns a string representation of the unsigned integer value. Syntax of the method is given below.
Syntax:
Example:
In this example, we are using toBinaryString() method to get binary of an int value. It effective way to get binary of an int value.
Â
5. valueOf()
The valueOf()
method is used to get an Integer object representing the specified int value. It takes a single int type argument and returns an Integer instance. Syntax of the method is given below.
Syntax:
Exmaple:
Â
6. parseInt()
The parseInt()
method is used to parse the specified string argument as a signed decimal integer. The characters in the string must all be decimal digits.
It takes a single argument of string type and returns an int value.
Syntax:
Example:
In the following example, we are passing a string that contains digits to parseInt()
method. The method returns an int value corresponding to the string.
Â
7. getInteger()
Syntax:
Example:
Â
8. decode()
The decode()
method is used to decode a String into an Integer. It takes an string type argument that can be a decimal, hexadecimal, and octal number and returns an instance of Integer class. Syntax of the method is given below.
Syntax:
Example:
In this example, we are using different types of numerical values to decode it and get integer instance.
Â
9. rotateLeft() and rotateRight()
These two methods are used to rotate the two’s complement binary representation of the specified int value left or right by the specified number of bits.
Syntax:
Example:
Â
Java Long class
The Long class is a wrapper class that is used to wrap a value of the primitive type long in an object. An object of type Long contains a single field whose type is long.
In addition, this class provides several methods for converting a long to a String and vice versa. Declaration of the class is given below.
Declaration
This class is located into java.lang package and java.base module. Here we are discussing methods of Long class with their examples.
1. toString()
This method returns a String object representing this Long’s value. The value is converted to signed decimal representation and returned as a string. It overrides toString()
method of Object class.
It does not take any argument but returns a string representation of the value of this object in base 10. Syntax of this method is given below.
Syntax:
Example:
Â
2. toHexString()
The toHexString()
method is used to get a string representation of the long argument.
This method takes an argument of long type and returns a hexadecimal string. Syntax of the method is given below.
Syntax:
3. toOctalString()
This method is helpful when we want to get an octal string representation of an long type value.
It can be used to get a string representation of the long argument.
It takes an long type argument and returns an unsigned string representation of the argument. Syntax of this method is given below.
Syntax:
4. toBinaryString()
The toBinaryString()
method is used to get a string representation of the long argument as an unsigned integer in base 2.
It takes an long argument and returns a string representation of the unsigned long value. Syntax of the method is given below.
Syntax:
Example:
We are getting hexadecimal string value of a long value using the toHexString()
method. In this example, we passed 25 as an argument and get its binary, hex and octal string. See the below example.
Â
5. valueOf()
The valueOf()
method is used to get an Long object representing the specified long value. It takes a single long type argument and returns a Long instance. Syntax of the method is given below.
Syntax:
Example:
Â
6. parseLong()
The parseLong()
method is used to parse the specified string argument as a signed decimal Long. The characters in the string must all be decimal digits.
It takes a single argument of string type and returns a Long value.
Syntax:
Example:
In the following example, we are passing a string that contains digits to parseLong()
method. The method returns a long value corresponding to the string.
Â
7. getLong()
The getLong()
method is used to determine the long value of the system property with the specified name. It takes a string argument that specifies name of a property and returns a Long value. Syntax of the method is given below.
Syntax:
Example:
Â
8.decode()
The decode()
method is used to decode a String into Long. It takes an string type argument that can be a decimal, hexadecimal, and octal number and returns an instance of Long class. Syntax of the method is given below.
Syntax:
Example:
In this example, we are using different types of numerical values to decode and get long instance.
Â
9. rotateLeft() and rotateRight()
These two methods are used to rotate the two’s complement binary representation of the specified long value left or right by the specified number of bits.
Syntax:
Example:
Â