Apr 12

In this java wrapper class tutorial, you will see definition of wrapper class with examples.

What is a wrapper class in java? Explain with examples.

In Java, a wrapper class is defined as a class in which a primitive value is wrapped up. These primitive wrapper classes are used to represent primitive data type values as objects. The Java platform provides wrapper classes for each of the primitive data types. For example, Integer wrapper class holds primitive ‘int’ data type value. Similarly, Float wrapper class contain ‘float’ primitive values, Character wrapper class holds a ‘char’ type value, and Boolean wrapper class represents ‘boolean’ value.

Wrapper Class Hierarchy Diagram

All of the numeric wrapper classes are subclasses of the abstract class java.lang.Number as shown in the above Wrapper Class Hierarchy diagram. There are four other subclasses of Number that are not shown here. BigDecimal and BigInteger are used for high-precision calculations. AtomicInteger and AtomicLong are used for multi-threaded applications. The remaining two wrapper classes Character & Boolean are direct subclasses of java.lang.Object.

Examples of Java Wrapper Classes:

To describe the application of wrapper classes in java, let us create an Integer wrapper class object with primitive int type value and get back the primitive value from the wrapper object.

        int i = 26;    // Primitive data type 'int'
        //  Integer Wrapper class instantiation
        Integer i_Obj = new Integer(i);
        // Unwrapping primitive data 'int' from wrapper object
        int i2 = i_Obj.intValue();    

List of all Wrapper Classes in java

The following table shows list of different types of wrapper classes in java:

Primitive data type Wrapper class
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

Why do we need wrapper classes in java?

Do we really need wrapper classes or wrapper data types in java? Can’t we use primitive data types directly instead of wrapper classes? Actually we can use primitive types in many places but not all. In some cases we can’t use primitive values as is, so wrapper classes are needed/required. For example, if we need to store numbers in a collection, we can’t use primitives because collections such as List, Set, and Map need objects as their elements. In such cases you must use wrapper classes.

What is the difference between primitive data types and wrapper classes in java?

The main difference between primitive data types and wrapper classes (wrapper types) in java is that the primitive types can be used as raw data for operations such as arithmetic, logical, etc. and wrapper classes acts as data holders for these primitive data types.

Important note: The primitive data type values will be stored in Stack Memory whereas wrapper class objects (like any other java objects) are stored in Heap Memory.

Use of wrapper class in java

Although, the primary purpose/use of wrapper classes in java is to wrap primitive values, there are few other uses/advantages of wrapper classes. One feature is to use constants defined by the wrapper class, such as MIN_VALUE and MAX_VALUE, that provide the upper and lower bounds of the data type, and the other application is to use wrapper class methods for converting values to and from other primitive data types, for converting to and from strings, and for converting between number systems (decimal, octal, hexadecimal, binary).

Are wrapper classes mutable in java?

A big “No”. Like Strings, wrapper classes in java such as Integer, Float, Long, and Double are also “immutable”. Once created, the numbers wrapped within the objects of wrapper classes in java.lang package can’t be changed. Read Why Strings & Wrapper classes are immutable and final in java for more details.

Summary

In this tutorial, i defined wrapper classes, explained use of wrapper classes, different types of wrapper classes (wrapper classes list), need for wrapper classes, difference between primitive data types and wrapper classes, and wrapper classes immutability. For further learning of java wrapper classes, read the java wrapper class tutorial Java Primitive Wrapper Class Tutorial: API Methods and Constructors.

Related posts:

  1. Is String Mutable in Java? Why Wrapper Classes are Immutable and Final in Java?
  2. Java Primitive Wrapper Class Tutorial: API Methods and Constructors
  3. Java Character Wrapper Class Tutorial: API Methods and Constructors
  4. Java Integer Wrapper Class Tutorial: API Methods and Constructors
  5. Explain Most Common Exceptions and Errors in Java with Examples
  6. What is a Java Exception? Explain Exception Handling in Java with Examples Program
  7. Java 5 (J2SE 5.0/JDK 1.5) New Features with Examples Tutorial 1
  8. Java 5 (J2SE 5.0/JDK 1.5) New Features with Examples Tutorial 2
  9. Explain String vs StringBuffer vs StringBuilder with toString() Example
  10. How to fix “Exception in thread main java.lang” Common Exceptions? – Tutorial 1

written by Seetha Ram Janapala
Tags: application of wrapper classes in java, boolean wrapper class, character wrapper class, character wrapper class in java, core java for beginners, core java tutorial, define wrapper class, example of wrapper class in java, features of wrapper class in java, integer wrapper class, integer wrapper class in java, integer wrapper class java, integer wrapper class java example, j2se 5.0, java wrapper class, java wrapper class example, java wrapper classes, list of wrapper classes in java, primitive wrapper class, primitive wrapper classes in java, purpose of wrapper class in java, usage of wrapper classes in java, use of wrapper class, use of wrapper class in java, uses of wrapper classes, wrapper class, wrapper class advantages in java, wrapper class definition, wrapper class example, wrapper class example in java, wrapper class in java, wrapper class in java example, wrapper class in java with example, wrapper class java, wrapper class tutorial, wrapper classes, wrapper classes in java, wrapper classes java


One Response to “Java Wrapper Class Tutorial: Explain Java Wrapper Classes with Examples”

  1. 1. java programming training for beginners Says:

    Its really a nice and helpful piece of information regarding wrapper class. I am happy that you just shared this helpful information with us. Please stay us informed like this. Thanks for sharing.

Leave a Reply

 
Copyright © 2014 Explain-Java.com. All rights reserved.