See the Tutorial List

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 datatypes, Wrapper classes are used.

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

There are 6 subclasses in Number class

number-class

Following are the methods of Number class with there example

1. xxx xxxValue()

This method is used for converting Number object into a primitive data type.

Syntax:

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

Example:

	
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.

2. int compareTo(NumberSubClass referenceName)

Syntax:

	
public int compareTo( NumberSubClass referenceName )
	

Example:

	
public class NumberDemo2
{ 
    public static void main(String[] args)  
    { 
        Integer a = new Integer("45"); 
        System.out.println(a.compareTo(5));  
        System.out.println(a.compareTo(55));  
        System.out.println(a.compareTo(45));  
    } 
}
	

number-class-example

3. boolean equals(Object obj)

Syntax :

	
public boolean equals(Object obj)
	

Example:

	
public class NumberDemo3
{ 
    public static void main(String[] args) 
    { 
        Short s = new Short("25"); 
        Short x = 20; 
        Integer y = 25;  
        Short z = 25; 
        System.out.println(s.equals(x)); 
        System.out.println(s.equals(y)); 
        System.out.println(s.equals(z)); 
    } 
}
	

number-class-example

4. int parseInt(String s,int radix)

Syntax :

	
static int parseInt(String s, int radix)
	

Example:

	
public class NumberDemo4
{ 
    public static void main(String[] args) 
    { 

        int a = Integer.parseInt("454",8);  
        int b = Integer.parseInt("-FF", 16); 
        long c = Long.parseLong("24523698745",10);  

        System.out.println(a); 
        System.out.println(b); 
        System.out.println(c); 

        int x = Integer.parseInt("studytonight",8);  

        int y = Integer.parseInt("99",8);  

    } 
}
	

number-class-example

5. int parseInt(String s)

Syntax :

	
static int parseInt(String s)
	

Example:

	
public class NumberDemo5
{ 
    public static void main(String[] args)  
    { 
        int a = Integer.parseInt("424");  
        long b = Long.parseLong("5632147569");  

        System.out.println(a); 
        System.out.println(b); 

        int x = Integer.parseInt("studytonight");  
        int y = Integer.parseInt("-FF");         
    } 
}
	

number-class-example

Integer class

Below are the Methods of Integer class and their example.

1. toString()

Syntax:

	
public String toString(int b)
	

Example:

	
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()

Syntax:

	
public String toHexString(int b)
	

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()

Syntax:

	
public String toOctalString(int b)
	

Example:

	
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()

Syntax:

	
public String toBinaryString(int b)
	

Example:

	 
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()

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()

Syntax:

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

Example:

	
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()

Syntax:

	
public static Integer decode(String s)
             throws NumberFormatException
	

Example:

	
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()

Syntax:

	
public static introtateLeft(intval, intdist)
	

10. rotateRight()

Syntax:

	
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


Long class

Below are the Methods of Long class and their example.

1. toString()

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()

Syntax:

	
public String toHexString(long b)
	

3. toOctalString()

Syntax:

	
public String toOctalString(long b)
	

4. toBinaryString()

Syntax:

	
public String toBinaryString(long b)
	

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()

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()

Syntax:

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

Example:

	
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()

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()

Syntax:

	
public static Long decode(String s)
            throws NumberFormatException
	

Example:

	
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()

Syntax:

	
public static long rotateLeft(long val, intdist)
	

10. rotateRight()

Syntax:

	
public static long rotateRight(long val, intdist)
	

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