Join us on :

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 suggests they are used to wrap the primitive data types into an 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 subclasses to Object Class, 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 into 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 parameter, converts the accepted string parameter into int and create Integer object for the int.


										Integer intObj2 = new Integer("89");
										


Instead of using the 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

  • As an argument of a method that expects an object (often used when manipulating collections of numbers).
  • 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.
  • 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
  • Wrapper classes convert numeric strings into numeric values.
  • The way to store primitive data in an object.
  • The valueOf() method is available in all wrapper classes except Character
  • All wrapper classes have typeValue() method. This method returns the value of the object as its primitive type.


Reasons for using Wrapper classes :

  • Allows null values
  • Can be used in the collections such as List, Map, etc.
  • Can be used in methods which accepts arguments of the Object type.
  • 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 :

Auto-boxing 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 :

Un-boxing 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 values 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

Article is written by Pavan (a) KarthiQ. Well, I am serving notice period in an MNC, Bangalore. I thought to enrich every person knowledge a little, I always have a feeling, when we teach something, we will learn more than what you know. Knowledge is the only thing that doubles when you spend it.

I have also created the reporter for Protractor Jasmine. Use for your projects without any hesitation

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