Primitive Type Wrapper Class
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
In Java, wrapper classes are used to wrap primitive data types into objects
To use primitives in Collections (e.g., ArrayList<Integer>)
For utility methods (e.g., [Link]())
For object-oriented programming — treat primitives as objects
To use null values, which primitives can't have
public class WrapperExample {
public static void main(String[] args) {
int num = 10;
// Manually boxing the int into an Integer object
Integer boxed = [Link](num);
// Unboxing the Integer object back to int
int unboxed = [Link]();
[Link]("Boxed: " + boxed);
[Link]("Unboxed: " + unboxed);
}
}
Autoboxing
Converting a primitive to a wrapper class automatically.
int a = 5;
Integer obj = a; // Autoboxing
Unboxing
Converting a wrapper class object to a primitive automatically.
Integer obj = 10;
int b = obj; // Unboxing
Wrapper Class Methods
Integer x = [Link]("123");
int y = [Link]("456");
[Link](x); // 123
[Link](y); // 456
1. valueOf()
Returns an Integer object representing the specified value.
Integer x = [Link](10); // from int
Integer y = [Link]("20"); // from String
2. parseInt()
Parses the string and returns a primitive int.
int a = [Link]("123"); // returns int
3. intValue()
Returns the value of the Integer object as a primitive int.
Integer obj = [Link](30);
int num = [Link](); // unboxing
4. toString()
Converts the Integer (or any wrapper) to a String.
Integer num = 45;
String str = [Link](); // "45"
5. compareTo()
Compares two Integer objects.
Integer a = 100;
Integer b = 200;
int result = [Link](b); // returns negative because a < b
6. compare()
Compares two primitive int values (static method).
int result = [Link](100, 200); // returns negative
7. equals()
Checks if two Integer objects are equal in value.
Integer a = 50;
Integer b = 50;
boolean isEqual = [Link](b); // true
8. decode()
Converts a string to an Integer. Supports decimal, hexadecimal, and octal.
Integer dec = [Link]("123"); // 123
Integer hex = [Link]("0x1A"); // 26
Integer oct = [Link]("075"); // 61 (octal)
9. MAX_VALUE and MIN_VALUE
Constants that define the max and min values an int can have.
[Link](Integer.MAX_VALUE); // 2147483647
[Link](Integer.MIN_VALUE); // -2147483648
10. reverse()
Returns the value obtained by reversing the order of the bits.
int rev = [Link](12); // reverses bits
public class WrapperMethodsExample {
public static void main(String[] args) {
// valueOf()
Integer a = [Link](100);
Integer b = [Link]("200");
// parseInt()
int c = [Link]("300");
// intValue()
int d = [Link]();
// toString()
String str = [Link]();
// compareTo() and compare()
int comp1 = [Link](b); // a vs b
int comp2 = [Link](500, 400);
// equals()
boolean isEqual = [Link]([Link](100));
// decode()
Integer hex = [Link]("0x10");
// MAX_VALUE and MIN_VALUE
int max = Integer.MAX_VALUE;
int min = Integer.MIN_VALUE;
// Output
[Link]("a: " + a);
[Link]("b: " + b);
[Link]("c: " + c);
[Link]("d: " + d);
[Link]("str: " + str);
[Link]("comp1: " + comp1);
[Link]("comp2: " + comp2);
[Link]("isEqual: " + isEqual);
[Link]("hex: " + hex);
[Link]("MAX: " + max);
[Link]("MIN: " + min);
}
}
Size of int vs Integer in Java
1. int (primitive type)
Size: Always 4 bytes = 32 bits
Fixed size, defined by the Java specification.
Stored directly in memory.
2. Integer (wrapper class)
Not just a number — it’s an object.
Internally wraps an int, but adds object metadata, headers, and methods.
The actual memory footprint is larger than 4 bytes due to:
o Object header (typically 8 to 16 bytes)
o The int value (4 bytes)
o Padding for memory alignment (possible)
📝 Approximate size of an Integer object in memory: 16 to 24 bytes,
depending on the JVM and architecture (32-bit or 64-bit).
Use primitives when possible for better performance
Use wrappers when:
You need to store null
You're using Collections (e.g., List<Integer>)
You need object methods
Why would you want to store null?
To represent "no value", missing data, or optional values
Especially useful in:
o Databases (nullable fields)
o Collections (e.g., List<Integer>)
o JSON serialization/deserialization
o Optional logic (e.g., something may or may not be present)
If a primitive field is missing in the JSON, it gets default values:
int → 0
boolean → false
double → 0.0