Wrapper Classes in Java: Definition & Example

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Inheritance in Java: Definition, Example & Syntax

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:04 Primitive Data Type &…
  • 2:11 Methods and Examples
  • 3:55 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

A wrapper is like a blanket over another Java data type, such as the primitive data types int, char, float, and double. This lesson will define the term and provide working code examples.

Primitive Data Type & Wrapper Class

Java provides several primitive data types. These include int (integer values), char (character), double (doubles/decimal values), and byte (single-byte values). Sometimes the primitive data type isn't enough and we may actually have to work with an integer object.

Let's first look at the wrapper classes. First, we'll list the Java primitive data type and then we'll explain its wrapper class.

Java Primitive Data Type Wrapper Class
int Integer
double Double
boolean Boolean
byte Byte
char Character
float Float
long Long
short Short

Notice that all wrapper classes start with a capital letter, such as Integer, while the primitive data types are lowercase, such as int.

Now let's look at basic syntax. There are a couple of ways to use the wrapper classes. The first is to simply declare them with the upper-case wrapper name, like what you're seeing below:


Java wrapper basic


This is perfectly valid, but it could be a little confusing because many of the wrapper classes share the name of the primitive data type. If you use a mix of primitive data types and wrapper classes, the code might be a little harder to maintain.

The other option is to use the wrapper class as you would other classes. That is, when you want to create a new instance of that class, use the new keyword to instantiate, which means to create, an object of that type. Take a look at the code below:


Java wrapper instantiate


In this image, you're seeing a new Integer object declared.

First, we created a primitive integer and gave it a value of 10. Next, we instantiated (created) a new instance of the Integer class and gave it the value of the primitive variable.

Methods and Examples

A really cool feature of the wrapper classes is that they include their own methods that can be used with each wrapper class. For example, you can show the double value of an integer variable, compare values, or even convert values to a string.

Let's look at a code example for the heavy hitter Integer.

Int Primitive = Integer Wrapper

The wrapper class for the int data type is the Integer class. Let's expand upon the previous example of the Integer and use one of the methods to convert it to a Double. The method to do this is doubleValue(), and the code looks like:


Java wrapper integer method


Now that we've created a new instance of Integer, let's look at some of the methods that come with these powerful tools in the Java toolbox.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support