Wrapper classes in Java

Wrapper classes in java is nothing but the classes which are used to convert the primitive data types into a Objects.

As their name suggest they are used to wrap the primitive data types into Object type.

The primitive data types are not objects so they do not belong to any class.

Collections only stores and supports objects, so it is required to convert the primitive type to object first, which we can do by using wrapper classes. wrapper-classes-java-selenium
All the wrapper classes are subclass to Obaject, All the number related classes ae sub-class to Number class. Number class is an abstract class in Java.

Note: There are four other subclasses of Number that are not discussed here. BigDecimal and BigInteger are used for high-precision calculations. AtomicInteger and AtomicLong are used for multi-threaded applications.

Boolean class (for boolean) :

Boolean wrapper class converts the boolean primitive data ino Boolean Object.

Number class (for number) :

All the number related classes are inherited from the Number class.

Character class (for character) :

Character wrapper class converts the character primitive values into character object.

Byte class (for byte) :

Byte wrapper class converts the byte value into Byte Object

Integer class (for int) :

Integer class converts the primitive value into Integer values, remaining wrapper classes also converts primitive values into respective Object value.

We will be discussing the methods present in the Integer class, almost all the remaining classes have similar methods only.

Integer Wrapper class

Integer class has two constructors: 1. Integer(int i) : accepts the integer parameter and creates the object for the int.


										Integer intObj1 = new Integer(10);
										


2. Integer(String s) : accepts String paramater, converts the accepted string parameter into int and create Integer object for the int.


										Integer intObj2 = new Integer("89");
										


Instead of using above two form it is always better to use below way of creating the object and which takes less memory.


										Integer i  = Integer.valueOf("100");
										

Below are few non-static methods present in the Integer class

byteValue() :

Returns the value of this Integer as a byte.

compareTo(Integer anotherInteger) :

Compares two Integer objects numerically.

intValue() :

Returns the value of this Integer as an int.

toString() :

Returns a String object representing this Integer's value. Below are few static methods present in the Integer class.

parseInt(String s) :

Parses the string argument as a signed decimal integer.

valueOf(String s) :

Returns an Integer object holding the value of the specified String.

toString(int i) :

Returns a String object representing the specified integer.

Reasons to use Number class than primitive values :
There are three reasons that you might use a Number object rather than a primitive
1. As an argument of a method that expects an object (often used when manipulating collections of numbers).

2. To use constants defined by the class, such as MIN_VALUE and MAX_VALUE, that provide the upper and lower bounds of the data type.

3. To use class methods for converting values to and from other primitive types, for converting to and from strings, and for converting between number systems (decimal, octal, hexadecimal, binary).

Features of the Java wrapper Classes
1. Wrapper classes convert numeric strings into numeric values.

2. The way to store primitive data in an object.

3. The valueOf() method is available in all wrapper classes except Character

4. All wrapper classes have typeValue() method. This method returns the value of the object as its primitive type.

Reasons for using Wrapper classes :
1. Allows null values

2. Can be used in collection such as List, Map, etc.

3. Can be used in methods which accepts arguments of Object type.

4. Makes available all the functions that Object class has such as clone(), equals(), hashCode(), toString() etc.

Boxing :

The process of converting the primitive data type into Object using Wrapper class is called as Boxing. For example converting int to Integer.

Auto-Boxing :

Autoboxing is nothing but converting primitive data into Object automatically, this is the reason we are able to store our primitive values in the Collections.

Un-Boxing :

Unboxing is the process of converting the primitive values which are wrapped as Object into primitive value.

Auto-Unboxing :

Auto-Unboxing is nothing but converting Objects into primitive values automatically. With respect to java boxing and Unboxing occurs automatically, so JVM uses boxing and unboxing auto-boxing and auto-unboxing to store the primitive valus in list and to retrieve from the list.


										import java.util.ArrayList;
										import java.util.List;

										public class IntegerExample {
											public static void main(String[] args) {
												// below is the process of converting int -> Integer
												// also known as boxing
												int i = 10; // primitive value
												Integer intObj = new Integer(10);
												
												// below is example for auto-boxing
												List al = new ArrayList();
												// syntax of adding a value to list
												// al.add(Object e);
												// but in place of Object we are able to store the int
												// int is not stored as int but as Integer 
												//and then converted into Object class object
												al.add(i);
												
												// unboxing
												intObj.intValue();
												
												// on below line int stored as object is Un-boxed and became as int
												// this is called as auto-unboxing
												System.out.println(al.get(0));
											}
										}
										

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
  • Prashant Jain
    Hi,
    
    Nice & knowledgable article!!!
    Have you worked on browsermob proxy?
    I have an query’s in browsermob proxy like how we can wait till the network layer of browser ends??
    
    Thanks
    Prashant
    Reply
    • karthiQ [ admin]
      Hi Prashant Jain,
      
      I have not worked on browsermob proxy but i would try to add an tutorial on it very soon.
      
      Reply
  • Copyright @