Signup/Sign In

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:

	
class WrapperPrimitiveDemo1 
{ 
    public static void main(String[] args) 
    { 
        Integer a = new Integer(12); 
        System.out.println("Old value = "+a); 
        xyz(a); 
        System.out.println("New Value = "+a); 
    } 
    private static void xyz(Integer a) 
    { 
        a = a + 10; 
    } 
}
	

wrapper-class

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:

	
byte byteValue()
short shortValue()
int intValue()
long longValue()
float floatValue()
double doubleValue()
	

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.

	
public class NumberDemo1
{ 
    public static void main(String[] args)  
    { 
        Double d1 = new Double("4.2365"); 
        byte b = d1.byteValue();  
        short s = d1.shortValue();  
        int i = d1.intValue();  
        long l = d1.longValue();  
        float f = d1.floatValue();  
        double d = d1.doubleValue();  

        System.out.println("converting Double to byte : " + b); 
        System.out.println("converting Double to short : " + s); 
        System.out.println("converting Double to int : " + i); 
        System.out.println("converting Double to long : " + l); 
        System.out.println("converting Double to float : " + f); 
        System.out.println("converting Double to double : " + d1); 
    } 
}
	

number-class-example.

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

public final class Integer extends Number implements Comparable<Integer>

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.

	
public String toString(int b)
	

Example:

In this example, we are using toString method to get string representation of Integer object.

	
public class IntegerClassDemo1  
{ 
    public static void main(String args[])  
    { 
		int a = 95;
        Integer x = new Integer(a); 
		System.out.println("toString(a) = " + Integer.toString(a));           
    } 
}
	

integer-class

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:

	
public String toHexString(int b)
	

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.

	
public class IntegerClassDemo1  
{ 
    public static void main(String args[])  
    { 
		int a = 95;
        Integer x = new Integer(a); 
		System.out.println("toHexString(a) = " + Integer.toHexString(a));           
    } 
}
	

integer-class-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:

	
public String toOctalString(int b)
	

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.

	
public class IntegerClassDemo1  
{ 
    public static void main(String args[])  
    { 
		int a = 95;
        Integer x = new Integer(a); 
		System.out.println("toOctalString(a) = " + Integer.toOctalString(a));           
    } 
}
	

integer-class-example

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:

	
public String toBinaryString(int b)
	

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.

	 
public class IntegerClassDemo1  
{ 
    public static void main(String args[])  
    { 
		int a = 95;
        Integer x = new Integer(a); 
		System.out.println("toBinaryString(a) = " + Integer.toBinaryString(a));           
    } 
}
	 

integer-class-example

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:

	
public static Integer valueOf(int b)
	

Exmaple:

	
public class IntegerClassDemo1  
{ 
    public static void main(String args[])  
    { 
		int a = 95;
        Integer x = Integer.valueOf(a); 
		System.out.println("valueOf(a) = " + x);           
    } 
}
	

integer-class-example

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:

	
public static intparseInt(String val, int radix) throws NumberFormatException
	

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.

	
public class IntegerClassDemo1  
{ 
    public static void main(String args[])  
    { 
        String a = "95";
        Integer x = Integer.parseInt(a); 
		System.out.println("parseInt(a) = " + x);           
    } 
}
	

integer-class-example

7. getInteger()

Syntax:

	
public static Integer getInteger(String prop)
	

Example:

	
public class IntegerClassDemo1  
{ 
    public static void main(String args[])  
    { 
		int a = Integer.getInteger("sun.arch.data.model");
		System.out.println("getInteger(sun.arch.data.model) = " + a);           
    } 
}
	

integer-class-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:

	
public static Integer decode(String s)
             throws NumberFormatException
	

Example:

In this example, we are using different types of numerical values to decode it and get integer instance.

	
public class IntegerClassDemo1  
{ 
    public static void main(String args[])  
    { 
        String a = "55";
		String b = "004";
		String c = "0x0f";

		Integer a1 = Integer.decode(a); 
		System.out.println("decode(55) = " + a1); 
		a1 = Integer.decode(b); 
		System.out.println("decode(004) = " + a1); 
		a1 = Integer.decode(c); 
		System.out.println("decode(0x0f) = " + a1);           
    } 
}
	

integer-class-example

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:

	
public static introtateLeft(intval, intdist)
public static introtateRight(intval, intdist)
	

Example:

	
public class IntegerClassDemo1  
{ 
    public static void main(String args[])  
    { 
        int a = 5;
        System.out.println("rotateLeft = "+ Integer.rotateLeft(a, 5));
        System.out.println("rotateRight = "+ Integer.rotateRight(a, 4));            
    } 
}
	

integer-class-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

public final class Long extends Number implements Comparable<Long>

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:

	
public String toString(long b)
	

Example:

	
public class LongDemo1
{
	public static void main(String as[])
	{
		long a = 25;
		System.out.println("toString(a) = " + Long.toString(a));
	}
}
	

long-class

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:

	
public String toHexString(long b)
	

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:

	
public String toOctalString(long b)
	

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:

	
public String toBinaryString(long b)
	

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.

	
public class LongDemo1
{
	public static void main(String as[])
	{
		long a = 25;
		System.out.println("toHexString(a) =" + Long.toHexString(a)); 
		System.out.println("toOctalString(a) =" + Long.toOctalString(a)); 
		System.out.println("toBinaryString(a) =" + Long.toBinaryString(a)); 
	}
}
	

long-class-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:

	
public static Long valueOf(long b)
	

Example:

	
public class LongDemo1
{
	public static void main(String as[])
	{
		long a = 25;
		String b = "45";
		Long x = Long.valueOf(a); 
		System.out.println("valueOf(a) = " + x); 
		x = Long.valueOf(b); 
		System.out.println("ValueOf(b) = " + x); 
		x = Long.valueOf(b, 6); 
		System.out.println("ValueOf(b,6) = " + x); 		
	}
}
	

long-class-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:

	
public static long parseInt(String val, int radix) throws NumberFormatException
	

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.

	
public class LongDemo1
{
	public static void main(String as[])
	{
		long a = 25;
		String b = "45";
		Long x = Long.parseLong(b); 
		System.out.println("parseLong(b) = " + x); 
		x = Long.parseLong(b, 6); 
		System.out.println("parseLong(b,6) = " + x); 		
	}
}
	

long-class-example

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:

	
public static Long getLong(String prop)
	

Example:

	
public class LongDemo1
{
	public static void main(String as[])
	{
		long a = Long.getLong("sun.arch.data.model"); 
		System.out.println("getLong(sun.arch.data.model) = " + a); 
		System.out.println("getLong(abcd) =" + Long.getLong("abcd"));
	}
}
	

long-class-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:

	
public static Long decode(String s) throws NumberFormatException
	

Example:

In this example, we are using different types of numerical values to decode and get long instance.

	
public class LongDemo1
{
	public static void main(String as[])
	{
	String a = "25"; 
        String b = "007"; 
        String c = "0x0f"; 

        Long x = Long.decode(a); 
		System.out.println("decode(25) = " + x); 
		x = Long.decode(b); 
		System.out.println("decode(007) = " + x); 
		x = Long.decode(c); 
		System.out.println("decode(0x0f) = " + x); 	
	}
}
	

long-class-example

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:

	
public static long rotateLeft(long val, int dist)       
public static long rotateRight(long val, int dist)
	

Example:

	
public class LongDemo1
{
    public static void main(String as[])
    {
	    long a = 3;
	    System.out.println("rotateLeft(0000 0000 0000 0011 , 3) =" + Long.rotateLeft(a, 3));    
	    System.out.println("rotateRight(0000 0000 0000 0011 , 3) =" + Long.rotateRight(a, 3));          
    }
}
	

long-class-example