X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

Wrapper Classes :

We know that all the command line arguments which we pass using our keyboard into Java program are by default available in main() method in the form of array of objects of String class and we cannot perform numerical operations on String type data. In order to perform numerical operations on such String type data we need to convert it into fundamental/numerical type data.

For each and every predefined data type there exists a predefined class which is known as wrapper class. The purpose of wrapper class is to convert numerical String type data into numerical type data. It wrap primitives into object form so that we can use primitives as an object.

X

wrap :

It refers to cover or enclose something.

In java.lang package we have eight wrapper classes :

Primitive data type        Wrapper Class

boolean                       Boolean
byte                          Byte
char                          Character 
short                         Short
int                           Integer
long                          Long
float                         Float
double                        Double
	   

Constructors of wrapper classes :


Creating wrapper objects :-

Almost all wrapper classes contain two constructors. One constructor can take corresponding primitive as argument and other can take String as argument.

	  

Example :

Integer i = new Integer(10); Integer i = new Integer("10"); Double d = new Double(10.5); Double d = new Double("10.5");

If the String is not properly formatted then we will get runtime error saying NumberFormatException.

	  

Example :

Integer i = new Integer("ten"); //Runtime error : NumberFormatException

Float class can contain three constructors. One can be of float primitive as argument or can be of String as argument or also can be of double primitive argument.

	  

Example :

Float f = new Float(10.5f); Float f = new Float("10.5f"); Float f = new Float(10.5);

Character class contains only one constructor which can take char primitive as argument.

	  

Example :

Character c = new Character('a'); // valid Character c = new Character("a"); // invalid (Compile time error : constructor Character in class Character cannot be applied to given types; required : char found : String reason: actual argument String cannot be converted to char by method invocation conversion)
X

invocation :

It refers to the action of invoking (call on) someone or something.

Boolean class contain two constructors. One can be of boolean primitive as argument and other can be of String as argument.

If we are passing boolean primitive as argument then only allowed values are true or false. If we try to provide any other argument type then we will get compile time error.

	  

Example :

Boolean b = new Boolean(true); // valid Boolean b = new Boolean(True); // invalid (Compile time error : cannot find symbol symbol: variable True) Boolean b = new Boolean("true"); // valid Boolean b = new Boolean("false"); // valid Boolean b = new Boolean("True"); // valid Boolean b = new Boolean("False"); // valid Boolean b = new Boolean("TRUE"); // valid Boolean b = new Boolean("FALSE"); // valid Boolean b = new Boolean("Algo"); // valid
Wrapper Class    Corresponding Constructor Argument

Byte             byte or String
Short            short or String
Integer          int or String
Long             long or String
Float            float or String or double
Double           double or String
Character        char
Boolean          boolean or String
	  

Example :

class Example { public static void main(String[ ] args) { Integer i1 = new Integer(10); Integer i2 = new Integer("10"); Double d1 = new Double(10.5); Double d2 = new Double("10.5"); Float f1 = new Float(10.5f); Float f2 = new Float("10.5f"); Float f3 = new Float(10.5); Character c = new Character('a'); Boolean b1 = new Boolean("true"); Boolean b2 = new Boolean("false"); Boolean b3 = new Boolean("True"); Boolean b4 = new Boolean("False"); Boolean b5 = new Boolean("TRUE"); Boolean b6 = new Boolean("FALSE"); Boolean b7 = new Boolean("Algo"); System.out.println(i1); System.out.println(i2); System.out.println(d1); System.out.println(d2); System.out.println(f1); System.out.println(f2); System.out.println(f3); System.out.println(c); System.out.println(b1); System.out.println(b2); System.out.println(b3); System.out.println(b4); System.out.println(b5); System.out.println(b6); System.out.println(b7); } }

Output :

10 10 10.5 10.5 10.5 10.5 10.5 a true false true false true false false

Utility methods :

There are four types of method :

  • valueOf()




  • xxxValue()




  • parseXxx()




  • toString()





valueOf() :-

We can use valueOf() method for creating wrapper object as an alternative to constructor.

Every wrapper class except Character class contains a static valueOf() method for converting String to wrapper object.

public static wrapper valueOf(String s);
	   

Example :

class Example { public static void main(String[ ] args) { Boolean b1 = Boolean.valueOf("true"); Byte b2 = Byte.valueOf("10"); Short s = Short.valueOf("10"); Integer i = Integer.valueOf("10"); Long l = Long.valueOf("10"); Float f = Float.valueOf("10.5f"); Double d = Double.valueOf("10.5"); System.out.println(b1); System.out.println(b2); System.out.println(s); System.out.println(i); System.out.println(l); System.out.println(f); System.out.println(d); } }

Output :

true 10 10 10 10 10.5 10.5

Every integral type wrapper class (Byte,Short,Integer,Long) contains the following valueOf() method to convert specified radix (base) String form to corresponding wrapper class.

public static wrapper valueOf(String s , int radix);
	   

Example :

class Example { public static void main(String[ ] args) { Byte b = Byte.valueOf("10" , 2); Short s = Short.valueOf("1010" , 2); Integer i = Integer.valueOf("101010" , 2); Long l = Long.valueOf("10101010" , 2); System.out.println(b); System.out.println(s); System.out.println(i); System.out.println(l); } }

Output :

2 10 42 170

Every wrapper class including Character class contains the following valueOf() method to convert primitive to corresponding wrapper object.

public static wrapper valueOf(primitive p);
	   

Example :

class Example { public static void main(String[ ] args) { Boolean b = Boolean.valueOf(true); Character c = Character.valueOf('a'); Integer i = Integer.valueOf(10); Long l = Long.valueOf(10); Float f = Float.valueOf(10.5f); Double d = Double.valueOf(10.5); System.out.println(b); System.out.println(c); System.out.println(i); System.out.println(l); System.out.println(f); System.out.println(d); } }

Output :

true a 10 10 10.5 10.5

xxxValue() :-

We can use xxxValue() method to convert wrapper object into primitives.

Every number type wrapper class contains the following xxxValue() methods :

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

Example :

class Example { public static void main(String[ ] args) { Double d = new Double(10.5); System.out.println(d.byteValue()); System.out.println(d.shortValue()); System.out.println(d.intValue()); System.out.println(d.longValue()); System.out.println(d.floatValue()); System.out.println(d.doubleValue()); } }

Output :

10 10 10 10 10.5 10.5

charValue() :-

Character class contains charValue() method to convert Character object into char primitive.

public char charValue();	   
	   

Example :

class Example { public static void main(String[ ] args) { Character c = new Character('$'); System.out.println(c.charValue()); } }

Output :

$

booleanValue() :-

Boolean class contains booleanValue() method to convert Boolean object into boolean primitive.

public boolean booleanValue();	   
	   

Example :

class Example { public static void main(String[ ] args) { Boolean b = new Boolean("true"); System.out.println(b.booleanValue()); } }

Output :

true

parseXxx() :-

We can use parseXxx() method to convert String into corresponding primitive.

Every wrapper class except Character class contain the following parseXxx() method to convert String to corresponding primitive.

public static primitive parseXxx(String s);	   
	   

Example :

class Example { public static void main(String[ ] args) { boolean b1 = Boolean.parseBoolean("true"); byte b2 = Byte.parseByte("10"); short s = Short.parseShort("10"); int i = Integer.parseInt("10"); long l = Long.parseLong("10"); float f = Float.parseFloat("10.5f"); double d = Double.parseDouble("10.5"); System.out.println(b1); System.out.println(b2); System.out.println(s); System.out.println(i); System.out.println(l); System.out.println(f); System.out.println(d); } }

Output :

true 10 10 10 10 10.5 10.5

Every integral type wrapper class contain the following parseXxx() method to convert specified radix (base) String into corresponding primitive.

public static primitive parseXxx(String s , int radix);	   
	   

Example :

class Example { public static void main(String[ ] args) { byte b = Byte.parseByte("10" , 2); short s = Short.parseShort("1010" , 2); int i = Integer.parseInt("101010" , 2); long l = Long.parseLong("10101010" , 2); System.out.println(b); System.out.println(s); System.out.println(i); System.out.println(l); } }

Output :

2 10 42 170

toString() :-

We can use toString() method to convert wrapper object or primitive into String.

Every wrapper class contain the following toString() method to convert wrapper object into String type.

public String toString();	   
	   

Example :

class Example { public static void main(String[ ] args) { Boolean b = new Boolean(true); Integer i = new Integer(10); Long l = new Long(10); Float f = new Float(10.5f); Double d = new Double(10.5); System.out.println(b.toString()); System.out.println(i.toString()); System.out.println(l.toString()); System.out.println(f.toString()); System.out.println(d.toString()); } }

Output :

true 10 10 10.5 10.5

Every wrapper class contain a static toString() method to convert primitive to String form.

public static String toString(primitive p);	   
	   

Example :

class Example { public static void main(String[ ] args) { System.out.println(Boolean.toString(true)); System.out.println(Integer.toString(10)); System.out.println(Long.toString(10)); System.out.println(Float.toString(10.5f)); System.out.println(Double.toString(10.5)); } }

Output :

true 10 10 10.5 10.5

Integer and Long classes contain toString() method to convert primitive to specified radix (base) String form.

public static String toString(primitive p , int radix);	   
	   

Example :

class Example { public static void main(String[ ] args) { System.out.println(Integer.toString(10 , 2)); System.out.println(Long.toString(100 , 2)); } }

Output :

1010 1100100

Integer and Long classes contain the following toXxxString() method also.

public static String toBinaryString(primitive p);	   
public static String toOctalString(primitive p);	  
public static String toHexString(primitive p);	  
	   

Example :

class Example { public static void main(String[ ] args) { System.out.println(Integer.toBinaryString(123)); System.out.println(Long.toBinaryString(123)); System.out.println(Integer.toOctalString(123)); System.out.println(Long.toOctalString(123)); System.out.println(Integer.toHexString(123)); System.out.println(Long.toHexString(123)); } }

Output :

1111011 1111011 173 173 7b 7b

Conclusion :

  • To convert primitive type or wrapper object into String type then we can use toString() method.

  • To convert String type or primitive type into wrapper object then we can use valueOf() method.

  • To convert String type into primitive type then we can use parseXxx() method.

  • To convert wrapper object into primitive type then we can use xxxValue() method.


X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.