Skip to content
QuizMaker logoQuizMaker
Activity
Java Programming: From Zero to Enterprise
3. Core APIs & I/O
1. Getting Started with Java & the JVM
2. Data Types & Variables
3. Control Flow: Ifs & Loops
4. String Manipulation in Depth
5. Methods (Functions) Architecture
6. Arrays & The Enhanced For Loop
7. User Input via Scanner
8. Mathematical Operations & The Math Class
9. Operators in Depth
10. Block Scope & Variable Lifecycles
11. Introduction to Object-Oriented Programming
12. Classes & Instances Deep Dive
13. Constructors
14. Encapsulation & The 'this' Keyword
15. Inheritance: Extending Functionality
16. Polymorphism & Method Overriding
17. Abstraction & Abstract Classes
18. Interfaces: The Ultimate Contract
19. Packages & Access Modifiers
20. Enums (Enumerations)
21. Exceptions: Handling Runtime Errors
22. The 'throw' and 'throws' keywords
23. Dates, Times, and Formatting
24. Enumerable Data Structures
25. LinkedLists: The Alternative
26. HashMaps: Key-Value Architecture
27. HashSets: The Art of Uniqueness
28. Iterator: Safe Collection Traversal
29. Wrapper Classes & Autoboxing
30. Basic File I/O
31. Generics: Type-Safe Templates
32. Lambda Expressions & Functional Interfaces
33. The Stream API: Functional Data Pipelines
34. Optional: Beating the NullPointerException
35. Multithreading & Concurrency Basics
36. JDBC: Connecting to SQL Databases
37. Annotations & Reflection
38. The JVM Garbage Collector
39. Introduction to Spring Boot
40. Unit Testing with JUnit
41. Java Collections for DSA
CONTENTS

29. Wrapper Classes & Autoboxing

Bridging the gap between Primitives and true Objects.

Feb 22, 20264 views0 likes0 fires
18px

[!NOTE] We briefly touched on Wrapper classes when discussing ArrayLists. Because Java Generics (<Integer>) can physically only handle Objects living on the Heap memory, they cannot process pure primitives like int or double.

Wrapper classes wrap the raw primitive data inside a full Object shell, granting them access to hundreds of methods.

The Wrapper Conversions

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

Creating Wrapper Objects

You can instantiate Wrapper classes just like any other object.

Integer myInt = 5;       // Automatically wraps it!
Double myDouble = 5.99;
Character myChar = 'A';

// Because they are fully-fledged Objects, you can call Methods on them!
System.out.println(myInt.intValue()); // Extracts the raw primitive back out.
System.out.println(myDouble.doubleValue());

// Converting an Integer object directly into a String representation!
String myString = myInt.toString();
System.out.println(myString.length()); // Outputs 1

Autoboxing and Unboxing

Historically, developers had to manually write Integer myInt = new Integer(5); and then invoke myInt.intValue() to extract the 5 out every time they did math.

To reduce boilerplate, modern Java compilers employ Autoboxing and Unboxing.

  • Autoboxing: The automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int directly to an Integer.
  • Unboxing: The aggressive automatic conversion of wrapper class to primitive.
// Autoboxing: 
// Java sees primitive '10', creates an 'Integer' object in Heap memory silently, and adds it!
ArrayList<Integer> li = new ArrayList<>();
li.add(10); 

// Unboxing: 
// Java retrieves the 'Integer' object, quietly extracts the intValue(), and does the raw math!
int result = li.get(0) + 15; 

[!CAUTION] Avoid Autoboxing in massive for loop mathematical calculations. Constructing and destroying 10,000,000 Integer objects in a tight loop is extremely CPU intensive compared to using raw int primitives.

Share this article

Share on TwitterShare on LinkedInShare on FacebookShare on WhatsAppShare on Email

Test your knowledge

Take a quick quiz based on this chapter.

mediumJava
Quiz: Wrappers
2 questions5 min

Continue Learning

30. Basic File I/O

Intermediate
14 min

31. Generics: Type-Safe Templates

Advanced
12 min

32. Lambda Expressions & Functional Interfaces

Advanced
14 min
Lesson 9 of 10 in 3. Core APIs & I/O
Previous in 3. Core APIs & I/O
28. Iterator: Safe Collection Traversal
Next in 3. Core APIs & I/O
30. Basic File I/O
← Back to Java Programming: From Zero to Enterprise
Back to Java Programming: From Zero to EnterpriseAll Categories