Wrapper-Classes-in-Java explained with diagram.pdf
1.
Wrapper Classes inJava
Transform primitive data types into powerful objects for advanced Java
programming
2.
Understanding Data Types
PrimitiveTypes
Built-in data types: byte, short,
int, long, float, double,
boolean, char
Memory Storage
Define how variable values are
stored in memory and function
return types
Limitations
Cannot be used as objects, limiting their functionality in object-
oriented programming
3.
What Are WrapperClasses?
Wrapper classes convert primitive data types into objects, enabling object-oriented functionality.
Object Wrapping
Encapsulate primitive values within class
objects from java.lang package
Field Storage
Each wrapper object contains a field
storing the primitive data type value
Reference Types
Enable primitive values to be referenced
as objects in memory
4.
The Eight WrapperClasses
Byte
Wraps byte primitive
Short
Wraps short primitive
Integer
Wraps int primitive
Long
Wraps long primitive
Float
Wraps float primitive
Double
Wraps double primitive
Boolean
Wraps boolean primitive
Character
Wraps char primitive
5.
Why Wrapper ClassesAre Essential
01
Serialization
Convert objects into streams for data persistence and transmission
02
Method Parameters
Pass by reference to modify original values in method calls
03
Synchronization
Enable multi-threaded operations with object-based synchronization
04
Collections Framework
Work with ArrayList, Vector, HashSet - all require objects, not primitives
6.
Creating Wrapper Objects
SimpleObject Creation
Integer intObject = 115;
Double doubleObject = 165.56;
Character charObject = 'D’;
System.out.println(intObject); //115
System.out.println(doubleObject); // 165.56
System.out.println(charObject); // D
Use wrapper class instead of primitive type to
create objects that hold values.
7.
Essential Wrapper Methods
MethodDescription
toString() Returns String representation of the value
valueOf() Returns wrapper object from primitive value
equals() Compares wrapper objects for equality
compareTo() Compares wrapper objects numerically
typeValue() Converts wrapper to primitive type
parseInt() Parses String to Integer type
All numeric wrapper classes extend the abstract Number class, providing consistent method implementations.
8.
Autoboxing: Automatic Conversion
PrimitiveValue
int num = 15;
Automatic Boxing
Java compiler converts automatically
Wrapper Object
Integer obj = num;
ArrayList<Integer> list = new ArrayList<>();list.add(15); // autoboxing happens
hereint num = list.get(0); // unboxing happens here
9.
Unboxing: Object toPrimitive
1 Wrapper Object
Character unboxing = 'D';
2 Automatic Unboxing
Compiler extracts primitive value
3 Primitive Variable
char D = unboxing;
Key Benefit: Since Java 5, no need to manually call intValue(),
doubleValue() methods for conversion.
10.
Wrapper Class Advantages
CollectionsSupport
Essential for ArrayList, HashMap, and other collection frameworks
Null Values
Can store null values, unlike primitives - crucial for real-world applications
Rich Functionality
Access to utility methods for parsing, comparison, and type conversion
Object Operations
Enable synchronization, serialization, and advanced object-oriented features