codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

56 / 65 topics
56Java Wrapper Classes57Java Generics58Java RegEx59Java Threads60Java Lambda
Tutorials/Java Programming/Java Wrapper Classes
☕Java Programming

Java Wrapper Classes

Updated 2026-05-12
30 min read

Java Wrapper Classes

In the world of object-oriented programming, everything is an object. This principle holds true for Java as well, where even primitive data types have their corresponding wrapper classes. These wrapper classes provide a way to use primitive data types in a more flexible manner, especially when dealing with collections and APIs that require objects.

Introduction

Java provides wrapper classes for each of its primitive data types. The most commonly used wrapper classes are Integer, Double, Character, and Boolean. These classes allow you to treat primitive values as objects, enabling operations like adding them to collections or using them in methods that expect objects.

Understanding wrapper classes is crucial because they play a significant role in Java's type system, especially when working with generics and collections. In this tutorial, we'll explore the Integer, Double, Character, and Boolean wrapper classes, as well as the concepts of autoboxing and unboxing.

Wrapper Classes Overview

Integer

The Integer class is a wrapper for the primitive data type int. It provides several useful methods for working with integer values, such as parsing strings to integers and converting integers to strings.

IntegerExample.java
1public class IntegerExample {
2 public static void main(String[] args) {
3 // Creating an Integer object
4 Integer num = new Integer(10);
5
6 // Converting Integer to int
7 int primitiveNum = num.intValue();
8
9 // Parsing a string to an integer
10 String str = "20";
11 int parsedInt = Integer.parseInt(str);
12
13 System.out.println("Integer Object: " + num);
14 System.out.println("Primitive int: " + primitiveNum);
15 System.out.println("Parsed Int from String: " + parsedInt);
16 };
17}
Output
Integer Object: 10
Primitive int: 10
Parsed Int from String: 20

Double

The Double class is a wrapper for the primitive data type double. It provides methods for converting between strings and double values, as well as for performing mathematical operations.

DoubleExample.java
1public class DoubleExample {
2 public static void main(String[] args) {
3 // Creating a Double object
4 Double num = new Double(3.14);
5
6 // Converting Double to double
7 double primitiveNum = num.doubleValue();
8
9 // Parsing a string to a double
10 String str = "2.718";
11 double parsedDouble = Double.parseDouble(str);
12
13 System.out.println("Double Object: " + num);
14 System.out.println("Primitive double: " + primitiveNum);
15 System.out.println("Parsed Double from String: " + parsedDouble);
16 }
17}
Output
Double Object: 3.14
Primitive double: 3.14
Parsed Double from String: 2.718

Character

The Character class is a wrapper for the primitive data type char. It provides methods for checking character properties, such as whether a character is a digit or a letter.

CharacterExample.java
1public class CharacterExample {
2 public static void main(String[] args) {
3 // Creating a Character object
4 Character ch = new Character('A');
5
6 // Converting Character to char
7 char primitiveCh = ch.charValue();
8
9 // Checking character properties
10 boolean isDigit = Character.isDigit(ch);
11 boolean isLetter = Character.isLetter(ch);
12
13 System.out.println("Character Object: " + ch);
14 System.out.println("Primitive char: " + primitiveCh);
15 System.out.println("Is Digit: " + isDigit);
16 System.out.println("Is Letter: " + isLetter);
17 }
18}
Output
Character Object: A
Primitive char: A
Is Digit: false
Is Letter: true

Boolean

The Boolean class is a wrapper for the primitive data type boolean. It provides methods for converting between strings and boolean values.

BooleanExample.java
1public class BooleanExample {
2 public static void main(String[] args) {
3 // Creating a Boolean object
4 Boolean bool = new Boolean(true);
5
6 // Converting Boolean to boolean
7 boolean primitiveBool = bool.booleanValue();
8
9 // Parsing a string to a boolean
10 String str = "true";
11 boolean parsedBoolean = Boolean.parseBoolean(str);
12
13 System.out.println("Boolean Object: " + bool);
14 System.out.println("Primitive boolean: " + primitiveBool);
15 System.out.println("Parsed Boolean from String: " + parsedBoolean);
16 }
17}
Output
Boolean Object: true
Primitive boolean: true
Parsed Boolean from String: true

Autoboxing and Unboxing

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer.

Unboxing is the reverse process—converting a wrapper class back to its corresponding primitive type.

AutoboxingExample.java
1public class AutoboxingExample {
2 public static void main(String[] args) {
3 // Autoboxing: int to Integer
4 Integer num = 10;
5
6 // Unboxing: Integer to int
7 int primitiveNum = num;
8
9 System.out.println("Autoboxed Integer: " + num);
10 System.out.println("Unboxed int: " + primitiveNum);
11 }
12}
Output
Autoboxed Integer: 10
Unboxed int: 10

Practical Example

Let's create a practical example that demonstrates the use of wrapper classes and autoboxing/unboxing. We'll create a simple program that reads user input, stores it in a list, and then processes the data.

PracticalExample.java
1import java.util.ArrayList;
2import java.util.List;
3import java.util.Scanner;
4
5public class PracticalExample {
6 public static void main(String[] args) {
7 Scanner scanner = new Scanner(System.in);
8 List<Integer> numbers = new ArrayList<>();
9
10 System.out.println("Enter integers (type 'done' to finish):");
11
12 while (scanner.hasNext()) {
13 if (scanner.hasNextInt()) {
14 int num = scanner.nextInt();
15 numbers.add(num); // Autoboxing
16 } else {
17 String input = scanner.next();
18 if (input.equalsIgnoreCase("done")) {
19 break;
20 }
21 }
22 }
23
24 System.out.println("You entered the following numbers:");
25 for (Integer number : numbers) { // Unboxing
26 System.out.println(number);
27 }
28
29 scanner.close();
30 }
31}
Terminal
$ javac PracticalExample.java
$ java PracticalExample
Enter integers (type 'done' to finish):
10 20 30 done
You entered the following numbers:
10
20
30

Summary

Wrapper ClassPrimitive Type
Integerint
Doubledouble
Characterchar
Booleanboolean
  • Autoboxing: Automatic conversion from primitive type to wrapper class.
  • Unboxing: Automatic conversion from wrapper class to primitive type.

What's Next?

In the next topic, we'll explore Java Generics. Generics provide a way to write more flexible and reusable code by allowing you to define classes, interfaces, and methods that operate on objects of various types without specifying their exact types. This will help us understand how wrapper classes fit into the broader context of Java's type system.

Stay tuned for our next tutorial where we'll dive deeper into Generics!


PreviousJava IteratorNext Java Generics

Recommended Gear

Java IteratorJava Generics