LESSON 12
3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Define wrapper classes in Java.
- Identify wrapper classes for primitive data types.
- Perform boxing and unboxing operations.
- Convert primitive values into objects.
- Use wrapper classes in Java applications.
3.2 Overview
Java is an object-oriented programming language, but primitive data types such as int, double, and char are not objects. Wrapper classes solve this limitation by allowing primitive values to be treated as objects.
This lesson introduces learners to wrapper classes and explains how wrapper classes support collections, object manipulation, and advanced Java programming features. Learners will also explore boxing and unboxing operations used when converting between primitive values and objects.
Wrapper classes are commonly used in:
- Java Collections Framework,
- enterprise systems,
- database applications,
- APIs,
- and object-oriented software development.
Understanding wrapper classes is important because many Java libraries and frameworks require objects instead of primitive values.
KT1201 — Introduction to Wrapper Classes
Wrapper classes are classes that wrap primitive data types into objects.
Each primitive type in Java has a corresponding wrapper class.
Primitive Types and Wrapper Classes
| Primitive Type | Wrapper Class |
|---|---|
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| char | Character |
| boolean | Boolean |
Example
Integer number = 100;
In this example:
- Integer is the wrapper class,
- 100 is wrapped into an object.
Importance of Wrapper Classes
Wrapper classes help developers:
- work with collections,
- convert data types,
- use utility methods,
- and support object-oriented programming.
KT1202 — Boxing
Boxing is the process of converting a primitive value into a wrapper object.
Java supports:
- manual boxing,
- and automatic boxing (autoboxing).
Manual Boxing Example
int number = 50;
Integer obj = Integer.valueOf(number);
Autoboxing Example
int number = 50;
Integer obj = number;
Explanation
Java automatically converts:
- primitive values,
- into wrapper objects.
Importance of Boxing
Boxing allows primitive values to:
- behave like objects,
- work with collections,
- and integrate with Java libraries.
KT1203 — Unboxing
Unboxing is the process of converting a wrapper object into a primitive value.
Java also supports automatic unboxing.
Manual Unboxing Example
Integer obj = Integer.valueOf(100);
int number = obj.intValue();
Automatic Unboxing Example
Integer obj = 100;
int number = obj;
Importance of Unboxing
Unboxing allows:
- calculations,
- arithmetic operations,
- and processing of wrapper objects as primitive values.
KT1204 — Wrapper Class Methods
Wrapper classes provide useful built-in methods.
parseInt() Method
Converts a string into an integer.
String value = "200";
int number = Integer.parseInt(value);
System.out.println(number);
Output
200
toString() Method
Converts values into strings.
int number = 500;
String text = Integer.toString(number);
System.out.println(text);
Output
500
Importance of Wrapper Methods
Wrapper methods help:
- process user input,
- perform conversions,
- validate data,
- and support application logic.
KT1205 — Wrapper Classes in Collections
Collections such as ArrayList require objects instead of primitive values.
Wrapper classes allow primitive values to be stored in collections.
Example
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(10);
numbers.add(20);
System.out.println(numbers);
}
}
Output
[10, 20]
Importance of Wrapper Classes in Collections
Wrapper classes:
- allow primitive values in collections,
- support object manipulation,
- and simplify data processing.
KT1206 — Real-World Use of Wrapper Classes
Wrapper classes are commonly used in:
- banking systems,
- inventory systems,
- enterprise applications,
- APIs,
- and database systems.
Examples:
- converting user input,
- validating numeric data,
- storing values in collections,
- and processing transaction data.
Wrapper classes support flexible and object-oriented application development.
3.5 Key Notes / Summary
- Wrapper classes convert primitive values into objects.
- Every primitive type has a corresponding wrapper class.
- Boxing converts primitives into objects.
- Unboxing converts objects into primitives.
- Wrapper classes provide useful conversion methods.
- Collections require wrapper objects instead of primitive values.
- Wrapper classes support object-oriented programming.