• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Array » What is Wrapper Class in Java With Example
Next →
← Prev

What is Wrapper Class in Java With Example

By Dinesh Thakur

Elements that can be added to the Vector must be of type java.lang.Object. In other words, vectors cannot handle primitive data types like int, float, char and double as they are not objects. So in order to work with primitive type variables as if they were objects, Java provides class for each of the primitive types. These classes are known as wrapper classes.

Wrapper Class in JavaA wrapper class wraps the value of a primitive type such as int or double into an object of that class. The Wrapper classes are part of java.lang package which is imported by default into all Java programs. These wrapper classes define an instance variable of that primitive data type and also provides useful constants and methods for conversion between the primitive data types and the objects.  The following table lists the wrapper classes for each of the eight primitive types.

Primitive typesWrapper class 
intInteger
shortShort
longLong
byteByte
floatFloat
doubleDouble
charCharacter
booleanBoolean

From the table, we notice that all of the wrapper classes except Integer and Character are named the same as their corresponding primitive type where the first letter of the name is capitalized.

There are several ways to create an instance of primitive variable wrapper class. The first way is to use one of the following two constructors provided by each Wrapper class.
• A constructor that takes a value of primitive type and creates an object of the corresponding wrapper class. For example: To convert a primitive int variable into an Integer wrapper object, we can instantiate the Integer object using Integer constructor as follows,

int a = 35;
Integer aObject = new Integer(a);

The first statement declares an int variable named a and initializes it with a value 35. The second statement, instantiates an Integer object and initializes it with a value stored in variable a (i.e. 35) and then reference to the object is assigned to the variable aObject.

• A constructor that converts a single string parameter into the object’s initial value (except Character,which has no such constructor). For example,

Double dObj = new Double("30.24");

will create a Double wrapper class object dObj which is initialized to a double value 30.24 which is passed as a String argument.
Another way to create a primitive variable wrapper class object is to use one of the valueOf () static methods defined in each class. As in case of constructors, these methods can also take either a primitive value or a string representation of a primitive value as an argument. For example, the statement ,

Integer obj = Integer.valueOf("1010",2);

creates an Integer object obj and initialize it with a integer value 26 (i.e decimal equivalent of binary number 1010). Similarly the statement,

Double dObject = Double.valueOf("25.87");

creates a Double object dObject and initialize it with a double value 25.87.

Each wrapper class contains useful methods and constant fields. Each one has a MIN_VALUE and a MAX_VALUE constant, which represents the minimum and maximum value respectively of the corresponding primitive type. Each wrapper class contains the following methods which are as follows

• static <type> parse<Type> (String s) : This method passes the string passed as an argument and returns the corresponding primitive type. Here <type> may be any of primitive types except char. The <type> written along with parse is same as that of <Type> but with first letter uppercase. For example: When used in association with int primitive type, it will look like

static int parseInt(String s)

Now let us consider the following statements,

String s = "1245";
int val = Integer.parseInt(s);

The second statement will assign an int value of 1245 to int type variable val.
Note that character class does not have this method as it does not have string converting constructor. Also, Boolean class has its own interpretation of the format of s.

The complete list of these methods is shown in the table.

Wrapper ClassMethod Signature Method Argument
Booleanstatic boolean parseBoolean( ..)String
Character– Not available ––
Bytestatic byte parseByte( ..)String or String and radix
Shortstatic short parseShort( ..)String or String and radix
Integerstatic int parseInt( ..)String or String and radix
Longstatic long parseLong( ..)String or String and radix
Floatstatic float parseFloat( ..)String
Doublestatic double parseDouble (..)double or String

In table, the radix is the numeric base which can be 2,8, 10 or 16 for binary, octal, decimal and hexadecimal numbers respectively.
• <type> <type>Value():This method returns the primitive value corresponding to the current wrapper object. Here, <type> may be any of the primitive types except char. For example, Consider the following statements,

Integer obj = new Integer(67);
int i = obj.intValue();

The second statement converts the wrapped Integer to int primitive type. Therefore, i will contain 67.
The Character class does not support this method.
• String toString():This method returns the string with the value of primitive wrapper object. All the numeric wrapper classes support this method. For example,

Double d = new Double("56.845");
String str = d.toString();

• static <Type> valueOf (String str): This method creates a new object of the specified wrapper class <Type> whose value is initialized with the value of string passed as an argument. For example,

Long lObject = Long.valueOf("25678972L");
assigns a value 25678972 to the Long object lObject.
In addition to these methods, each wrapper class individually supports many other methods. You can get details from the Internet.The following program shows use of wrapper class methods.
public class IntWrapperClass {  
 public static void main(String[] args) {   
  int a=35;  
  Integer aObject=new Integer(a);  
  System.out.println("Maximum value of integer is : "+Integer.MAX_VALUE);  
  System.out.println("Maximum value of integer is : "+Integer.MIN_VALUE);  
  System.out.println("Integer.parseInt(\"1245\") : "+Integer.parseInt("1245"));  
  System.out.println("aObject.intValue() : "+aObject.intValue());  
  System.out.println("aObject.toString() : "+aObject.toString());  
  Integer Obj=Integer.valueOf("11010",2);  
  System.out.println("Obj.inValue() : "+Obj.intValue());  
 } 
}

Wrapper Class in Java With Example

You’ll also like:

  1. Type Wrapper in java
  2. StringBuffer Class in Java Example
  3. What is Java Strings? | String Class and its Methods with Examples
  4. Write A C++ Program To Make A Call Class Constructor Or Not During Class Array Declaration.
  5. Inner class in java with Example
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW