Data Types
Wrapper Classes
Objects for Primitives
You're building a list of test scores, but List<int> doesn't compile in Java.
Collections require objects, not primitives. Wrapper classes like Integer solve
this by wrapping primitives in objects.
Integer vs int
Wrapper classes are objects that contain primitive values.
public class IntegerWrapper {
public static void main(String[] args) {
// Primitive
int primitiveInt = 42;
// Wrapper object (explicit)
Integer wrapperInt = Integer.valueOf(42);
// They work the same in most cases
System.out.println("Primitive: " + primitiveInt);
System.out.println("Wrapper: " + wrapperInt);
// But wrapper is an object with methods
System.out.println("As hex: " + Integer.toHexString(primitiveInt));
System.out.println("As binary: " + Integer.toBinaryString(primitiveInt));
// Comparison
int a = 5;
Integer b = 5;
System.out.println("a == b: " + (a == b)); // Works due to unboxing
}
}
Integer is an object. int is a primitive. Use wrappers when you need objects.
Autoboxing and unboxing
Java automatically converts between primitives and wrappers.
import java.util.ArrayList;
public class Autoboxing {
public static void main(String[] args) {
// Autoboxing: int → Integer (automatic)
Integer boxed = 100;
System.out.println("Autoboxed: " + boxed);
// Unboxing: Integer → int (automatic)
int unboxed = boxed;
System.out.println("Unboxed: " + unboxed);
// Required for collections
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10); // autoboxing
numbers.add(20);
numbers.add(30);
int first = numbers.get(0); // unboxing
System.out.println("First element: " + first);
// Sum using unboxing
int sum = 0;
for (Integer num : numbers) {
sum += num; // unboxing in +=
}
System.out.println("Sum: " + sum);
}
}
Parse methods
Wrapper classes provide methods to parse strings.
public class ParseMethods {
public static void main(String[] args) {
// Parse methods from wrapper classes
String intStr = ;
String doubleStr = ;
String boolStr = ;
int intVal = Integer.parseInt(intStr);
double doubleVal = Double.parseDouble(doubleStr);
boolean boolVal = Boolean.parseBoolean(boolStr);
System.out.println("Parsed int: " + intVal);
System.out.println("Parsed double: " + doubleVal);
System.out.println("Parsed boolean: " + boolVal);
// Parse with radix (base)
String hexStr = "FF";
String binaryStr = "1010";
int fromHex = Integer.parseInt(hexStr, 16);
int fromBinary = Integer.parseInt(binaryStr, 2);
System.out.println("0xFF = " + fromHex);
System.out.println("0b1010 = " + fromBinary);
// valueOf returns wrapper, parseInt returns primitive
Integer obj = Integer.valueOf("42");
int prim = Integer.parseInt("42");
}
}
public class ParseMethods {
public static void main(String[] args) {
// Parse methods from wrapper classes
String intStr = ;
String doubleStr = ;
String boolStr = ;
int intVal = Integer.parseInt(intStr);
double doubleVal = Double.parseDouble(doubleStr);
boolean boolVal = Boolean.parseBoolean(boolStr);
System.out.println("Parsed int: " + intVal);
System.out.println("Parsed double: " + doubleVal);
System.out.println("Parsed boolean: " + boolVal);
// Parse with radix (base)
String hexStr = "FF";
String binaryStr = "1010";
int fromHex = Integer.parseInt(hexStr, 16);
int fromBinary = Integer.parseInt(binaryStr, 2);
System.out.println("0xFF = " + fromHex);
System.out.println("0b1010 = " + fromBinary);
// valueOf returns wrapper, parseInt returns primitive
Integer obj = Integer.valueOf("42");
int prim = Integer.parseInt("42");
}
}
public class ParseMethods {
public static void main(String[] args) {
// Parse methods from wrapper classes
String intStr = ;
String doubleStr = ;
String boolStr = ;
int intVal = Integer.parseInt(intStr);
double doubleVal = Double.parseDouble(doubleStr);
boolean boolVal = Boolean.parseBoolean(boolStr);
System.out.println("Parsed int: " + intVal);
System.out.println("Parsed double: " + doubleVal);
System.out.println("Parsed boolean: " + boolVal);
// Parse with radix (base)
String hexStr = "FF";
String binaryStr = "1010";
int fromHex = Integer.parseInt(hexStr, 16);
int fromBinary = Integer.parseInt(binaryStr, 2);
System.out.println("0xFF = " + fromHex);
System.out.println("0b1010 = " + fromBinary);
// valueOf returns wrapper, parseInt returns primitive
Integer obj = Integer.valueOf("42");
int prim = Integer.parseInt("42");
}
}
public class ParseMethods {
public static void main(String[] args) {
// Parse methods from wrapper classes
String intStr = ;
String doubleStr = ;
String boolStr = ;
int intVal = Integer.parseInt(intStr);
double doubleVal = Double.parseDouble(doubleStr);
boolean boolVal = Boolean.parseBoolean(boolStr);
System.out.println("Parsed int: " + intVal);
System.out.println("Parsed double: " + doubleVal);
System.out.println("Parsed boolean: " + boolVal);
// Parse with radix (base)
String hexStr = "FF";
String binaryStr = "1010";
int fromHex = Integer.parseInt(hexStr, 16);
int fromBinary = Integer.parseInt(binaryStr, 2);
System.out.println("0xFF = " + fromHex);
System.out.println("0b1010 = " + fromBinary);
// valueOf returns wrapper, parseInt returns primitive
Integer obj = Integer.valueOf("42");
int prim = Integer.parseInt("42");
}
}
public class ParseMethods {
public static void main(String[] args) {
// Parse methods from wrapper classes
String intStr = ;
String doubleStr = ;
String boolStr = ;
int intVal = Integer.parseInt(intStr);
double doubleVal = Double.parseDouble(doubleStr);
boolean boolVal = Boolean.parseBoolean(boolStr);
System.out.println("Parsed int: " + intVal);
System.out.println("Parsed double: " + doubleVal);
System.out.println("Parsed boolean: " + boolVal);
// Parse with radix (base)
String hexStr = "FF";
String binaryStr = "1010";
int fromHex = Integer.parseInt(hexStr, 16);
int fromBinary = Integer.parseInt(binaryStr, 2);
System.out.println("0xFF = " + fromHex);
System.out.println("0b1010 = " + fromBinary);
// valueOf returns wrapper, parseInt returns primitive
Integer obj = Integer.valueOf("42");
int prim = Integer.parseInt("42");
}
}
public class ParseMethods {
public static void main(String[] args) {
// Parse methods from wrapper classes
String intStr = ;
String doubleStr = ;
String boolStr = ;
int intVal = Integer.parseInt(intStr);
double doubleVal = Double.parseDouble(doubleStr);
boolean boolVal = Boolean.parseBoolean(boolStr);
System.out.println("Parsed int: " + intVal);
System.out.println("Parsed double: " + doubleVal);
System.out.println("Parsed boolean: " + boolVal);
// Parse with radix (base)
String hexStr = "FF";
String binaryStr = "1010";
int fromHex = Integer.parseInt(hexStr, 16);
int fromBinary = Integer.parseInt(binaryStr, 2);
System.out.println("0xFF = " + fromHex);
System.out.println("0b1010 = " + fromBinary);
// valueOf returns wrapper, parseInt returns primitive
Integer obj = Integer.valueOf("42");
int prim = Integer.parseInt("42");
}
}
public class ParseMethods {
public static void main(String[] args) {
// Parse methods from wrapper classes
String intStr = ;
String doubleStr = ;
String boolStr = ;
int intVal = Integer.parseInt(intStr);
double doubleVal = Double.parseDouble(doubleStr);
boolean boolVal = Boolean.parseBoolean(boolStr);
System.out.println("Parsed int: " + intVal);
System.out.println("Parsed double: " + doubleVal);
System.out.println("Parsed boolean: " + boolVal);
// Parse with radix (base)
String hexStr = "FF";
String binaryStr = "1010";
int fromHex = Integer.parseInt(hexStr, 16);
int fromBinary = Integer.parseInt(binaryStr, 2);
System.out.println("0xFF = " + fromHex);
System.out.println("0b1010 = " + fromBinary);
// valueOf returns wrapper, parseInt returns primitive
Integer obj = Integer.valueOf("42");
int prim = Integer.parseInt("42");
}
}
public class ParseMethods {
public static void main(String[] args) {
// Parse methods from wrapper classes
String intStr = ;
String doubleStr = ;
String boolStr = ;
int intVal = Integer.parseInt(intStr);
double doubleVal = Double.parseDouble(doubleStr);
boolean boolVal = Boolean.parseBoolean(boolStr);
System.out.println("Parsed int: " + intVal);
System.out.println("Parsed double: " + doubleVal);
System.out.println("Parsed boolean: " + boolVal);
// Parse with radix (base)
String hexStr = "FF";
String binaryStr = "1010";
int fromHex = Integer.parseInt(hexStr, 16);
int fromBinary = Integer.parseInt(binaryStr, 2);
System.out.println("0xFF = " + fromHex);
System.out.println("0b1010 = " + fromBinary);
// valueOf returns wrapper, parseInt returns primitive
Integer obj = Integer.valueOf("42");
int prim = Integer.parseInt("42");
}
}
public class ParseMethods {
public static void main(String[] args) {
// Parse methods from wrapper classes
String intStr = ;
String doubleStr = ;
String boolStr = ;
int intVal = Integer.parseInt(intStr);
double doubleVal = Double.parseDouble(doubleStr);
boolean boolVal = Boolean.parseBoolean(boolStr);
System.out.println("Parsed int: " + intVal);
System.out.println("Parsed double: " + doubleVal);
System.out.println("Parsed boolean: " + boolVal);
// Parse with radix (base)
String hexStr = "FF";
String binaryStr = "1010";
int fromHex = Integer.parseInt(hexStr, 16);
int fromBinary = Integer.parseInt(binaryStr, 2);
System.out.println("0xFF = " + fromHex);
System.out.println("0b1010 = " + fromBinary);
// valueOf returns wrapper, parseInt returns primitive
Integer obj = Integer.valueOf("42");
int prim = Integer.parseInt("42");
}
}
public class ParseMethods {
public static void main(String[] args) {
// Parse methods from wrapper classes
String intStr = ;
String doubleStr = ;
String boolStr = ;
int intVal = Integer.parseInt(intStr);
double doubleVal = Double.parseDouble(doubleStr);
boolean boolVal = Boolean.parseBoolean(boolStr);
System.out.println("Parsed int: " + intVal);
System.out.println("Parsed double: " + doubleVal);
System.out.println("Parsed boolean: " + boolVal);
// Parse with radix (base)
String hexStr = "FF";
String binaryStr = "1010";
int fromHex = Integer.parseInt(hexStr, 16);
int fromBinary = Integer.parseInt(binaryStr, 2);
System.out.println("0xFF = " + fromHex);
System.out.println("0b1010 = " + fromBinary);
// valueOf returns wrapper, parseInt returns primitive
Integer obj = Integer.valueOf("42");
int prim = Integer.parseInt("42");
}
}
public class ParseMethods {
public static void main(String[] args) {
// Parse methods from wrapper classes
String intStr = ;
String doubleStr = ;
String boolStr = ;
int intVal = Integer.parseInt(intStr);
double doubleVal = Double.parseDouble(doubleStr);
boolean boolVal = Boolean.parseBoolean(boolStr);
System.out.println("Parsed int: " + intVal);
System.out.println("Parsed double: " + doubleVal);
System.out.println("Parsed boolean: " + boolVal);
// Parse with radix (base)
String hexStr = "FF";
String binaryStr = "1010";
int fromHex = Integer.parseInt(hexStr, 16);
int fromBinary = Integer.parseInt(binaryStr, 2);
System.out.println("0xFF = " + fromHex);
System.out.println("0b1010 = " + fromBinary);
// valueOf returns wrapper, parseInt returns primitive
Integer obj = Integer.valueOf("42");
int prim = Integer.parseInt("42");
}
}
public class ParseMethods {
public static void main(String[] args) {
// Parse methods from wrapper classes
String intStr = ;
String doubleStr = ;
String boolStr = ;
int intVal = Integer.parseInt(intStr);
double doubleVal = Double.parseDouble(doubleStr);
boolean boolVal = Boolean.parseBoolean(boolStr);
System.out.println("Parsed int: " + intVal);
System.out.println("Parsed double: " + doubleVal);
System.out.println("Parsed boolean: " + boolVal);
// Parse with radix (base)
String hexStr = "FF";
String binaryStr = "1010";
int fromHex = Integer.parseInt(hexStr, 16);
int fromBinary = Integer.parseInt(binaryStr, 2);
System.out.println("0xFF = " + fromHex);
System.out.println("0b1010 = " + fromBinary);
// valueOf returns wrapper, parseInt returns primitive
Integer obj = Integer.valueOf("42");
int prim = Integer.parseInt("42");
}
}
public class ParseMethods {
public static void main(String[] args) {
// Parse methods from wrapper classes
String intStr = ;
String doubleStr = ;
String boolStr = ;
int intVal = Integer.parseInt(intStr);
double doubleVal = Double.parseDouble(doubleStr);
boolean boolVal = Boolean.parseBoolean(boolStr);
System.out.println("Parsed int: " + intVal);
System.out.println("Parsed double: " + doubleVal);
System.out.println("Parsed boolean: " + boolVal);
// Parse with radix (base)
String hexStr = "FF";
String binaryStr = "1010";
int fromHex = Integer.parseInt(hexStr, 16);
int fromBinary = Integer.parseInt(binaryStr, 2);
System.out.println("0xFF = " + fromHex);
System.out.println("0b1010 = " + fromBinary);
// valueOf returns wrapper, parseInt returns primitive
Integer obj = Integer.valueOf("42");
int prim = Integer.parseInt("42");
}
}
public class ParseMethods {
public static void main(String[] args) {
// Parse methods from wrapper classes
String intStr = ;
String doubleStr = ;
String boolStr = ;
int intVal = Integer.parseInt(intStr);
double doubleVal = Double.parseDouble(doubleStr);
boolean boolVal = Boolean.parseBoolean(boolStr);
System.out.println("Parsed int: " + intVal);
System.out.println("Parsed double: " + doubleVal);
System.out.println("Parsed boolean: " + boolVal);
// Parse with radix (base)
String hexStr = "FF";
String binaryStr = "1010";
int fromHex = Integer.parseInt(hexStr, 16);
int fromBinary = Integer.parseInt(binaryStr, 2);
System.out.println("0xFF = " + fromHex);
System.out.println("0b1010 = " + fromBinary);
// valueOf returns wrapper, parseInt returns primitive
Integer obj = Integer.valueOf("42");
int prim = Integer.parseInt("42");
}
}
public class ParseMethods {
public static void main(String[] args) {
// Parse methods from wrapper classes
String intStr = ;
String doubleStr = ;
String boolStr = ;
int intVal = Integer.parseInt(intStr);
double doubleVal = Double.parseDouble(doubleStr);
boolean boolVal = Boolean.parseBoolean(boolStr);
System.out.println("Parsed int: " + intVal);
System.out.println("Parsed double: " + doubleVal);
System.out.println("Parsed boolean: " + boolVal);
// Parse with radix (base)
String hexStr = "FF";
String binaryStr = "1010";
int fromHex = Integer.parseInt(hexStr, 16);
int fromBinary = Integer.parseInt(binaryStr, 2);
System.out.println("0xFF = " + fromHex);
System.out.println("0b1010 = " + fromBinary);
// valueOf returns wrapper, parseInt returns primitive
Integer obj = Integer.valueOf("42");
int prim = Integer.parseInt("42");
}
}
public class ParseMethods {
public static void main(String[] args) {
// Parse methods from wrapper classes
String intStr = ;
String doubleStr = ;
String boolStr = ;
int intVal = Integer.parseInt(intStr);
double doubleVal = Double.parseDouble(doubleStr);
boolean boolVal = Boolean.parseBoolean(boolStr);
System.out.println("Parsed int: " + intVal);
System.out.println("Parsed double: " + doubleVal);
System.out.println("Parsed boolean: " + boolVal);
// Parse with radix (base)
String hexStr = "FF";
String binaryStr = "1010";
int fromHex = Integer.parseInt(hexStr, 16);
int fromBinary = Integer.parseInt(binaryStr, 2);
System.out.println("0xFF = " + fromHex);
System.out.println("0b1010 = " + fromBinary);
// valueOf returns wrapper, parseInt returns primitive
Integer obj = Integer.valueOf("42");
int prim = Integer.parseInt("42");
}
}
public class ParseMethods {
public static void main(String[] args) {
// Parse methods from wrapper classes
String intStr = ;
String doubleStr = ;
String boolStr = ;
int intVal = Integer.parseInt(intStr);
double doubleVal = Double.parseDouble(doubleStr);
boolean boolVal = Boolean.parseBoolean(boolStr);
System.out.println("Parsed int: " + intVal);
System.out.println("Parsed double: " + doubleVal);
System.out.println("Parsed boolean: " + boolVal);
// Parse with radix (base)
String hexStr = "FF";
String binaryStr = "1010";
int fromHex = Integer.parseInt(hexStr, 16);
int fromBinary = Integer.parseInt(binaryStr, 2);
System.out.println("0xFF = " + fromHex);
System.out.println("0b1010 = " + fromBinary);
// valueOf returns wrapper, parseInt returns primitive
Integer obj = Integer.valueOf("42");
int prim = Integer.parseInt("42");
}
}
public class ParseMethods {
public static void main(String[] args) {
// Parse methods from wrapper classes
String intStr = ;
String doubleStr = ;
String boolStr = ;
int intVal = Integer.parseInt(intStr);
double doubleVal = Double.parseDouble(doubleStr);
boolean boolVal = Boolean.parseBoolean(boolStr);
System.out.println("Parsed int: " + intVal);
System.out.println("Parsed double: " + doubleVal);
System.out.println("Parsed boolean: " + boolVal);
// Parse with radix (base)
String hexStr = "FF";
String binaryStr = "1010";
int fromHex = Integer.parseInt(hexStr, 16);
int fromBinary = Integer.parseInt(binaryStr, 2);
System.out.println("0xFF = " + fromHex);
System.out.println("0b1010 = " + fromBinary);
// valueOf returns wrapper, parseInt returns primitive
Integer obj = Integer.valueOf("42");
int prim = Integer.parseInt("42");
}
}
public class ParseMethods {
public static void main(String[] args) {
// Parse methods from wrapper classes
String intStr = ;
String doubleStr = ;
String boolStr = ;
int intVal = Integer.parseInt(intStr);
double doubleVal = Double.parseDouble(doubleStr);
boolean boolVal = Boolean.parseBoolean(boolStr);
System.out.println("Parsed int: " + intVal);
System.out.println("Parsed double: " + doubleVal);
System.out.println("Parsed boolean: " + boolVal);
// Parse with radix (base)
String hexStr = "FF";
String binaryStr = "1010";
int fromHex = Integer.parseInt(hexStr, 16);
int fromBinary = Integer.parseInt(binaryStr, 2);
System.out.println("0xFF = " + fromHex);
System.out.println("0b1010 = " + fromBinary);
// valueOf returns wrapper, parseInt returns primitive
Integer obj = Integer.valueOf("42");
int prim = Integer.parseInt("42");
}
}
public class ParseMethods {
public static void main(String[] args) {
// Parse methods from wrapper classes
String intStr = ;
String doubleStr = ;
String boolStr = ;
int intVal = Integer.parseInt(intStr);
double doubleVal = Double.parseDouble(doubleStr);
boolean boolVal = Boolean.parseBoolean(boolStr);
System.out.println("Parsed int: " + intVal);
System.out.println("Parsed double: " + doubleVal);
System.out.println("Parsed boolean: " + boolVal);
// Parse with radix (base)
String hexStr = "FF";
String binaryStr = "1010";
int fromHex = Integer.parseInt(hexStr, 16);
int fromBinary = Integer.parseInt(binaryStr, 2);
System.out.println("0xFF = " + fromHex);
System.out.println("0b1010 = " + fromBinary);
// valueOf returns wrapper, parseInt returns primitive
Integer obj = Integer.valueOf("42");
int prim = Integer.parseInt("42");
}
}
public class ParseMethods {
public static void main(String[] args) {
// Parse methods from wrapper classes
String intStr = ;
String doubleStr = ;
String boolStr = ;
int intVal = Integer.parseInt(intStr);
double doubleVal = Double.parseDouble(doubleStr);
boolean boolVal = Boolean.parseBoolean(boolStr);
System.out.println("Parsed int: " + intVal);
System.out.println("Parsed double: " + doubleVal);
System.out.println("Parsed boolean: " + boolVal);
// Parse with radix (base)
String hexStr = "FF";
String binaryStr = "1010";
int fromHex = Integer.parseInt(hexStr, 16);
int fromBinary = Integer.parseInt(binaryStr, 2);
System.out.println("0xFF = " + fromHex);
System.out.println("0b1010 = " + fromBinary);
// valueOf returns wrapper, parseInt returns primitive
Integer obj = Integer.valueOf("42");
int prim = Integer.parseInt("42");
}
}
public class ParseMethods {
public static void main(String[] args) {
// Parse methods from wrapper classes
String intStr = ;
String doubleStr = ;
String boolStr = ;
int intVal = Integer.parseInt(intStr);
double doubleVal = Double.parseDouble(doubleStr);
boolean boolVal = Boolean.parseBoolean(boolStr);
System.out.println("Parsed int: " + intVal);
System.out.println("Parsed double: " + doubleVal);
System.out.println("Parsed boolean: " + boolVal);
// Parse with radix (base)
String hexStr = "FF";
String binaryStr = "1010";
int fromHex = Integer.parseInt(hexStr, 16);
int fromBinary = Integer.parseInt(binaryStr, 2);
System.out.println("0xFF = " + fromHex);
System.out.println("0b1010 = " + fromBinary);
// valueOf returns wrapper, parseInt returns primitive
Integer obj = Integer.valueOf("42");
int prim = Integer.parseInt("42");
}
}
public class ParseMethods {
public static void main(String[] args) {
// Parse methods from wrapper classes
String intStr = ;
String doubleStr = ;
String boolStr = ;
int intVal = Integer.parseInt(intStr);
double doubleVal = Double.parseDouble(doubleStr);
boolean boolVal = Boolean.parseBoolean(boolStr);
System.out.println("Parsed int: " + intVal);
System.out.println("Parsed double: " + doubleVal);
System.out.println("Parsed boolean: " + boolVal);
// Parse with radix (base)
String hexStr = "FF";
String binaryStr = "1010";
int fromHex = Integer.parseInt(hexStr, 16);
int fromBinary = Integer.parseInt(binaryStr, 2);
System.out.println("0xFF = " + fromHex);
System.out.println("0b1010 = " + fromBinary);
// valueOf returns wrapper, parseInt returns primitive
Integer obj = Integer.valueOf("42");
int prim = Integer.parseInt("42");
}
}
public class ParseMethods {
public static void main(String[] args) {
// Parse methods from wrapper classes
String intStr = ;
String doubleStr = ;
String boolStr = ;
int intVal = Integer.parseInt(intStr);
double doubleVal = Double.parseDouble(doubleStr);
boolean boolVal = Boolean.parseBoolean(boolStr);
System.out.println("Parsed int: " + intVal);
System.out.println("Parsed double: " + doubleVal);
System.out.println("Parsed boolean: " + boolVal);
// Parse with radix (base)
String hexStr = "FF";
String binaryStr = "1010";
int fromHex = Integer.parseInt(hexStr, 16);
int fromBinary = Integer.parseInt(binaryStr, 2);
System.out.println("0xFF = " + fromHex);
System.out.println("0b1010 = " + fromBinary);
// valueOf returns wrapper, parseInt returns primitive
Integer obj = Integer.valueOf("42");
int prim = Integer.parseInt("42");
}
}
public class ParseMethods {
public static void main(String[] args) {
// Parse methods from wrapper classes
String intStr = ;
String doubleStr = ;
String boolStr = ;
int intVal = Integer.parseInt(intStr);
double doubleVal = Double.parseDouble(doubleStr);
boolean boolVal = Boolean.parseBoolean(boolStr);
System.out.println("Parsed int: " + intVal);
System.out.println("Parsed double: " + doubleVal);
System.out.println("Parsed boolean: " + boolVal);
// Parse with radix (base)
String hexStr = "FF";
String binaryStr = "1010";
int fromHex = Integer.parseInt(hexStr, 16);
int fromBinary = Integer.parseInt(binaryStr, 2);
System.out.println("0xFF = " + fromHex);
System.out.println("0b1010 = " + fromBinary);
// valueOf returns wrapper, parseInt returns primitive
Integer obj = Integer.valueOf("42");
int prim = Integer.parseInt("42");
}
}
public class ParseMethods {
public static void main(String[] args) {
// Parse methods from wrapper classes
String intStr = ;
String doubleStr = ;
String boolStr = ;
int intVal = Integer.parseInt(intStr);
double doubleVal = Double.parseDouble(doubleStr);
boolean boolVal = Boolean.parseBoolean(boolStr);
System.out.println("Parsed int: " + intVal);
System.out.println("Parsed double: " + doubleVal);
System.out.println("Parsed boolean: " + boolVal);
// Parse with radix (base)
String hexStr = "FF";
String binaryStr = "1010";
int fromHex = Integer.parseInt(hexStr, 16);
int fromBinary = Integer.parseInt(binaryStr, 2);
System.out.println("0xFF = " + fromHex);
System.out.println("0b1010 = " + fromBinary);
// valueOf returns wrapper, parseInt returns primitive
Integer obj = Integer.valueOf("42");
int prim = Integer.parseInt("42");
}
}
public class ParseMethods {
public static void main(String[] args) {
// Parse methods from wrapper classes
String intStr = ;
String doubleStr = ;
String boolStr = ;
int intVal = Integer.parseInt(intStr);
double doubleVal = Double.parseDouble(doubleStr);
boolean boolVal = Boolean.parseBoolean(boolStr);
System.out.println("Parsed int: " + intVal);
System.out.println("Parsed double: " + doubleVal);
System.out.println("Parsed boolean: " + boolVal);
// Parse with radix (base)
String hexStr = "FF";
String binaryStr = "1010";
int fromHex = Integer.parseInt(hexStr, 16);
int fromBinary = Integer.parseInt(binaryStr, 2);
System.out.println("0xFF = " + fromHex);
System.out.println("0b1010 = " + fromBinary);
// valueOf returns wrapper, parseInt returns primitive
Integer obj = Integer.valueOf("42");
int prim = Integer.parseInt("42");
}
}
Integer.parseInt(), Double.parseDouble() - these come from wrapper classes.
Null handling
Unlike primitives, wrapper objects can be null - useful but requires care.
public class Null {
public static void main(String[] args) {
// Wrapper can be null (primitive cannot)
Integer maybeAge = null; // Unknown age
// int age = null; // ERROR: won't compile
// Check before using
if (maybeAge != null) {
System.out.println("Age: " + maybeAge);
} else {
System.out.println("Age unknown");
}
// NullPointerException danger!
Integer quantity = null;
// int q = quantity; // Runtime error: NullPointerException
// Safe unboxing
int safeQuantity = (quantity != null) ? quantity : 0;
System.out.println("Safe quantity: " + safeQuantity);
// Practical: optional values
Integer[] scores = {85, null, 92, null, 78};
int count = 0;
int total = 0;
for (Integer score : scores) {
if (score != null) {
total += score;
count++;
}
}
System.out.println("Average (excluding missing): " + (total / count));
}
}
Useful methods
Wrapper classes provide utility methods beyond conversion.
public class Utilities {
public static void main(String[] args) {
// Constants
System.out.println("=== Integer Constants ===");
System.out.println("MAX_VALUE: " + Integer.MAX_VALUE);
System.out.println("MIN_VALUE: " + Integer.MIN_VALUE);
System.out.println("BYTES: " + Integer.BYTES);
// Comparison
int a = ;
int b = ;
System.out.println("\n=== Comparison ===");
System.out.println("max(" + a + ", " + b + "): " + Integer.max(a, b));
System.out.println("min(" + a + ", " + b + "): " + Integer.min(a, b));
System.out.println("compare(" + a + ", " + b + "): " + Integer.compare(a, b));
// Unsigned operations
System.out.println("\n=== Unsigned ===");
int negative = -1;
System.out.println("-1 as unsigned: " + Integer.toUnsignedString(negative));
// Character utilities
System.out.println("\n=== Character ===");
char c = ;
System.out.println("isDigit('" + c + "'): " + Character.isDigit(c));
System.out.println("isLetter('" + c + "'): " + Character.isLetter(c));
System.out.println("isUpperCase('" + c + "'): " + Character.isUpperCase(c));
System.out.println("toLowerCase('" + c + "'): " + Character.toLowerCase(c));
}
}
public class Utilities {
public static void main(String[] args) {
// Constants
System.out.println("=== Integer Constants ===");
System.out.println("MAX_VALUE: " + Integer.MAX_VALUE);
System.out.println("MIN_VALUE: " + Integer.MIN_VALUE);
System.out.println("BYTES: " + Integer.BYTES);
// Comparison
int a = ;
int b = ;
System.out.println("\n=== Comparison ===");
System.out.println("max(" + a + ", " + b + "): " + Integer.max(a, b));
System.out.println("min(" + a + ", " + b + "): " + Integer.min(a, b));
System.out.println("compare(" + a + ", " + b + "): " + Integer.compare(a, b));
// Unsigned operations
System.out.println("\n=== Unsigned ===");
int negative = -1;
System.out.println("-1 as unsigned: " + Integer.toUnsignedString(negative));
// Character utilities
System.out.println("\n=== Character ===");
char c = ;
System.out.println("isDigit('" + c + "'): " + Character.isDigit(c));
System.out.println("isLetter('" + c + "'): " + Character.isLetter(c));
System.out.println("isUpperCase('" + c + "'): " + Character.isUpperCase(c));
System.out.println("toLowerCase('" + c + "'): " + Character.toLowerCase(c));
}
}
public class Utilities {
public static void main(String[] args) {
// Constants
System.out.println("=== Integer Constants ===");
System.out.println("MAX_VALUE: " + Integer.MAX_VALUE);
System.out.println("MIN_VALUE: " + Integer.MIN_VALUE);
System.out.println("BYTES: " + Integer.BYTES);
// Comparison
int a = ;
int b = ;
System.out.println("\n=== Comparison ===");
System.out.println("max(" + a + ", " + b + "): " + Integer.max(a, b));
System.out.println("min(" + a + ", " + b + "): " + Integer.min(a, b));
System.out.println("compare(" + a + ", " + b + "): " + Integer.compare(a, b));
// Unsigned operations
System.out.println("\n=== Unsigned ===");
int negative = -1;
System.out.println("-1 as unsigned: " + Integer.toUnsignedString(negative));
// Character utilities
System.out.println("\n=== Character ===");
char c = ;
System.out.println("isDigit('" + c + "'): " + Character.isDigit(c));
System.out.println("isLetter('" + c + "'): " + Character.isLetter(c));
System.out.println("isUpperCase('" + c + "'): " + Character.isUpperCase(c));
System.out.println("toLowerCase('" + c + "'): " + Character.toLowerCase(c));
}
}
public class Utilities {
public static void main(String[] args) {
// Constants
System.out.println("=== Integer Constants ===");
System.out.println("MAX_VALUE: " + Integer.MAX_VALUE);
System.out.println("MIN_VALUE: " + Integer.MIN_VALUE);
System.out.println("BYTES: " + Integer.BYTES);
// Comparison
int a = ;
int b = ;
System.out.println("\n=== Comparison ===");
System.out.println("max(" + a + ", " + b + "): " + Integer.max(a, b));
System.out.println("min(" + a + ", " + b + "): " + Integer.min(a, b));
System.out.println("compare(" + a + ", " + b + "): " + Integer.compare(a, b));
// Unsigned operations
System.out.println("\n=== Unsigned ===");
int negative = -1;
System.out.println("-1 as unsigned: " + Integer.toUnsignedString(negative));
// Character utilities
System.out.println("\n=== Character ===");
char c = ;
System.out.println("isDigit('" + c + "'): " + Character.isDigit(c));
System.out.println("isLetter('" + c + "'): " + Character.isLetter(c));
System.out.println("isUpperCase('" + c + "'): " + Character.isUpperCase(c));
System.out.println("toLowerCase('" + c + "'): " + Character.toLowerCase(c));
}
}
public class Utilities {
public static void main(String[] args) {
// Constants
System.out.println("=== Integer Constants ===");
System.out.println("MAX_VALUE: " + Integer.MAX_VALUE);
System.out.println("MIN_VALUE: " + Integer.MIN_VALUE);
System.out.println("BYTES: " + Integer.BYTES);
// Comparison
int a = ;
int b = ;
System.out.println("\n=== Comparison ===");
System.out.println("max(" + a + ", " + b + "): " + Integer.max(a, b));
System.out.println("min(" + a + ", " + b + "): " + Integer.min(a, b));
System.out.println("compare(" + a + ", " + b + "): " + Integer.compare(a, b));
// Unsigned operations
System.out.println("\n=== Unsigned ===");
int negative = -1;
System.out.println("-1 as unsigned: " + Integer.toUnsignedString(negative));
// Character utilities
System.out.println("\n=== Character ===");
char c = ;
System.out.println("isDigit('" + c + "'): " + Character.isDigit(c));
System.out.println("isLetter('" + c + "'): " + Character.isLetter(c));
System.out.println("isUpperCase('" + c + "'): " + Character.isUpperCase(c));
System.out.println("toLowerCase('" + c + "'): " + Character.toLowerCase(c));
}
}
public class Utilities {
public static void main(String[] args) {
// Constants
System.out.println("=== Integer Constants ===");
System.out.println("MAX_VALUE: " + Integer.MAX_VALUE);
System.out.println("MIN_VALUE: " + Integer.MIN_VALUE);
System.out.println("BYTES: " + Integer.BYTES);
// Comparison
int a = ;
int b = ;
System.out.println("\n=== Comparison ===");
System.out.println("max(" + a + ", " + b + "): " + Integer.max(a, b));
System.out.println("min(" + a + ", " + b + "): " + Integer.min(a, b));
System.out.println("compare(" + a + ", " + b + "): " + Integer.compare(a, b));
// Unsigned operations
System.out.println("\n=== Unsigned ===");
int negative = -1;
System.out.println("-1 as unsigned: " + Integer.toUnsignedString(negative));
// Character utilities
System.out.println("\n=== Character ===");
char c = ;
System.out.println("isDigit('" + c + "'): " + Character.isDigit(c));
System.out.println("isLetter('" + c + "'): " + Character.isLetter(c));
System.out.println("isUpperCase('" + c + "'): " + Character.isUpperCase(c));
System.out.println("toLowerCase('" + c + "'): " + Character.toLowerCase(c));
}
}
public class Utilities {
public static void main(String[] args) {
// Constants
System.out.println("=== Integer Constants ===");
System.out.println("MAX_VALUE: " + Integer.MAX_VALUE);
System.out.println("MIN_VALUE: " + Integer.MIN_VALUE);
System.out.println("BYTES: " + Integer.BYTES);
// Comparison
int a = ;
int b = ;
System.out.println("\n=== Comparison ===");
System.out.println("max(" + a + ", " + b + "): " + Integer.max(a, b));
System.out.println("min(" + a + ", " + b + "): " + Integer.min(a, b));
System.out.println("compare(" + a + ", " + b + "): " + Integer.compare(a, b));
// Unsigned operations
System.out.println("\n=== Unsigned ===");
int negative = -1;
System.out.println("-1 as unsigned: " + Integer.toUnsignedString(negative));
// Character utilities
System.out.println("\n=== Character ===");
char c = ;
System.out.println("isDigit('" + c + "'): " + Character.isDigit(c));
System.out.println("isLetter('" + c + "'): " + Character.isLetter(c));
System.out.println("isUpperCase('" + c + "'): " + Character.isUpperCase(c));
System.out.println("toLowerCase('" + c + "'): " + Character.toLowerCase(c));
}
}
public class Utilities {
public static void main(String[] args) {
// Constants
System.out.println("=== Integer Constants ===");
System.out.println("MAX_VALUE: " + Integer.MAX_VALUE);
System.out.println("MIN_VALUE: " + Integer.MIN_VALUE);
System.out.println("BYTES: " + Integer.BYTES);
// Comparison
int a = ;
int b = ;
System.out.println("\n=== Comparison ===");
System.out.println("max(" + a + ", " + b + "): " + Integer.max(a, b));
System.out.println("min(" + a + ", " + b + "): " + Integer.min(a, b));
System.out.println("compare(" + a + ", " + b + "): " + Integer.compare(a, b));
// Unsigned operations
System.out.println("\n=== Unsigned ===");
int negative = -1;
System.out.println("-1 as unsigned: " + Integer.toUnsignedString(negative));
// Character utilities
System.out.println("\n=== Character ===");
char c = ;
System.out.println("isDigit('" + c + "'): " + Character.isDigit(c));
System.out.println("isLetter('" + c + "'): " + Character.isLetter(c));
System.out.println("isUpperCase('" + c + "'): " + Character.isUpperCase(c));
System.out.println("toLowerCase('" + c + "'): " + Character.toLowerCase(c));
}
}
public class Utilities {
public static void main(String[] args) {
// Constants
System.out.println("=== Integer Constants ===");
System.out.println("MAX_VALUE: " + Integer.MAX_VALUE);
System.out.println("MIN_VALUE: " + Integer.MIN_VALUE);
System.out.println("BYTES: " + Integer.BYTES);
// Comparison
int a = ;
int b = ;
System.out.println("\n=== Comparison ===");
System.out.println("max(" + a + ", " + b + "): " + Integer.max(a, b));
System.out.println("min(" + a + ", " + b + "): " + Integer.min(a, b));
System.out.println("compare(" + a + ", " + b + "): " + Integer.compare(a, b));
// Unsigned operations
System.out.println("\n=== Unsigned ===");
int negative = -1;
System.out.println("-1 as unsigned: " + Integer.toUnsignedString(negative));
// Character utilities
System.out.println("\n=== Character ===");
char c = ;
System.out.println("isDigit('" + c + "'): " + Character.isDigit(c));
System.out.println("isLetter('" + c + "'): " + Character.isLetter(c));
System.out.println("isUpperCase('" + c + "'): " + Character.isUpperCase(c));
System.out.println("toLowerCase('" + c + "'): " + Character.toLowerCase(c));
}
}
public class Utilities {
public static void main(String[] args) {
// Constants
System.out.println("=== Integer Constants ===");
System.out.println("MAX_VALUE: " + Integer.MAX_VALUE);
System.out.println("MIN_VALUE: " + Integer.MIN_VALUE);
System.out.println("BYTES: " + Integer.BYTES);
// Comparison
int a = ;
int b = ;
System.out.println("\n=== Comparison ===");
System.out.println("max(" + a + ", " + b + "): " + Integer.max(a, b));
System.out.println("min(" + a + ", " + b + "): " + Integer.min(a, b));
System.out.println("compare(" + a + ", " + b + "): " + Integer.compare(a, b));
// Unsigned operations
System.out.println("\n=== Unsigned ===");
int negative = -1;
System.out.println("-1 as unsigned: " + Integer.toUnsignedString(negative));
// Character utilities
System.out.println("\n=== Character ===");
char c = ;
System.out.println("isDigit('" + c + "'): " + Character.isDigit(c));
System.out.println("isLetter('" + c + "'): " + Character.isLetter(c));
System.out.println("isUpperCase('" + c + "'): " + Character.isUpperCase(c));
System.out.println("toLowerCase('" + c + "'): " + Character.toLowerCase(c));
}
}
public class Utilities {
public static void main(String[] args) {
// Constants
System.out.println("=== Integer Constants ===");
System.out.println("MAX_VALUE: " + Integer.MAX_VALUE);
System.out.println("MIN_VALUE: " + Integer.MIN_VALUE);
System.out.println("BYTES: " + Integer.BYTES);
// Comparison
int a = ;
int b = ;
System.out.println("\n=== Comparison ===");
System.out.println("max(" + a + ", " + b + "): " + Integer.max(a, b));
System.out.println("min(" + a + ", " + b + "): " + Integer.min(a, b));
System.out.println("compare(" + a + ", " + b + "): " + Integer.compare(a, b));
// Unsigned operations
System.out.println("\n=== Unsigned ===");
int negative = -1;
System.out.println("-1 as unsigned: " + Integer.toUnsignedString(negative));
// Character utilities
System.out.println("\n=== Character ===");
char c = ;
System.out.println("isDigit('" + c + "'): " + Character.isDigit(c));
System.out.println("isLetter('" + c + "'): " + Character.isLetter(c));
System.out.println("isUpperCase('" + c + "'): " + Character.isUpperCase(c));
System.out.println("toLowerCase('" + c + "'): " + Character.toLowerCase(c));
}
}
public class Utilities {
public static void main(String[] args) {
// Constants
System.out.println("=== Integer Constants ===");
System.out.println("MAX_VALUE: " + Integer.MAX_VALUE);
System.out.println("MIN_VALUE: " + Integer.MIN_VALUE);
System.out.println("BYTES: " + Integer.BYTES);
// Comparison
int a = ;
int b = ;
System.out.println("\n=== Comparison ===");
System.out.println("max(" + a + ", " + b + "): " + Integer.max(a, b));
System.out.println("min(" + a + ", " + b + "): " + Integer.min(a, b));
System.out.println("compare(" + a + ", " + b + "): " + Integer.compare(a, b));
// Unsigned operations
System.out.println("\n=== Unsigned ===");
int negative = -1;
System.out.println("-1 as unsigned: " + Integer.toUnsignedString(negative));
// Character utilities
System.out.println("\n=== Character ===");
char c = ;
System.out.println("isDigit('" + c + "'): " + Character.isDigit(c));
System.out.println("isLetter('" + c + "'): " + Character.isLetter(c));
System.out.println("isUpperCase('" + c + "'): " + Character.isUpperCase(c));
System.out.println("toLowerCase('" + c + "'): " + Character.toLowerCase(c));
}
}
public class Utilities {
public static void main(String[] args) {
// Constants
System.out.println("=== Integer Constants ===");
System.out.println("MAX_VALUE: " + Integer.MAX_VALUE);
System.out.println("MIN_VALUE: " + Integer.MIN_VALUE);
System.out.println("BYTES: " + Integer.BYTES);
// Comparison
int a = ;
int b = ;
System.out.println("\n=== Comparison ===");
System.out.println("max(" + a + ", " + b + "): " + Integer.max(a, b));
System.out.println("min(" + a + ", " + b + "): " + Integer.min(a, b));
System.out.println("compare(" + a + ", " + b + "): " + Integer.compare(a, b));
// Unsigned operations
System.out.println("\n=== Unsigned ===");
int negative = -1;
System.out.println("-1 as unsigned: " + Integer.toUnsignedString(negative));
// Character utilities
System.out.println("\n=== Character ===");
char c = ;
System.out.println("isDigit('" + c + "'): " + Character.isDigit(c));
System.out.println("isLetter('" + c + "'): " + Character.isLetter(c));
System.out.println("isUpperCase('" + c + "'): " + Character.isUpperCase(c));
System.out.println("toLowerCase('" + c + "'): " + Character.toLowerCase(c));
}
}
public class Utilities {
public static void main(String[] args) {
// Constants
System.out.println("=== Integer Constants ===");
System.out.println("MAX_VALUE: " + Integer.MAX_VALUE);
System.out.println("MIN_VALUE: " + Integer.MIN_VALUE);
System.out.println("BYTES: " + Integer.BYTES);
// Comparison
int a = ;
int b = ;
System.out.println("\n=== Comparison ===");
System.out.println("max(" + a + ", " + b + "): " + Integer.max(a, b));
System.out.println("min(" + a + ", " + b + "): " + Integer.min(a, b));
System.out.println("compare(" + a + ", " + b + "): " + Integer.compare(a, b));
// Unsigned operations
System.out.println("\n=== Unsigned ===");
int negative = -1;
System.out.println("-1 as unsigned: " + Integer.toUnsignedString(negative));
// Character utilities
System.out.println("\n=== Character ===");
char c = ;
System.out.println("isDigit('" + c + "'): " + Character.isDigit(c));
System.out.println("isLetter('" + c + "'): " + Character.isLetter(c));
System.out.println("isUpperCase('" + c + "'): " + Character.isUpperCase(c));
System.out.println("toLowerCase('" + c + "'): " + Character.toLowerCase(c));
}
}
public class Utilities {
public static void main(String[] args) {
// Constants
System.out.println("=== Integer Constants ===");
System.out.println("MAX_VALUE: " + Integer.MAX_VALUE);
System.out.println("MIN_VALUE: " + Integer.MIN_VALUE);
System.out.println("BYTES: " + Integer.BYTES);
// Comparison
int a = ;
int b = ;
System.out.println("\n=== Comparison ===");
System.out.println("max(" + a + ", " + b + "): " + Integer.max(a, b));
System.out.println("min(" + a + ", " + b + "): " + Integer.min(a, b));
System.out.println("compare(" + a + ", " + b + "): " + Integer.compare(a, b));
// Unsigned operations
System.out.println("\n=== Unsigned ===");
int negative = -1;
System.out.println("-1 as unsigned: " + Integer.toUnsignedString(negative));
// Character utilities
System.out.println("\n=== Character ===");
char c = ;
System.out.println("isDigit('" + c + "'): " + Character.isDigit(c));
System.out.println("isLetter('" + c + "'): " + Character.isLetter(c));
System.out.println("isUpperCase('" + c + "'): " + Character.isUpperCase(c));
System.out.println("toLowerCase('" + c + "'): " + Character.toLowerCase(c));
}
}
public class Utilities {
public static void main(String[] args) {
// Constants
System.out.println("=== Integer Constants ===");
System.out.println("MAX_VALUE: " + Integer.MAX_VALUE);
System.out.println("MIN_VALUE: " + Integer.MIN_VALUE);
System.out.println("BYTES: " + Integer.BYTES);
// Comparison
int a = ;
int b = ;
System.out.println("\n=== Comparison ===");
System.out.println("max(" + a + ", " + b + "): " + Integer.max(a, b));
System.out.println("min(" + a + ", " + b + "): " + Integer.min(a, b));
System.out.println("compare(" + a + ", " + b + "): " + Integer.compare(a, b));
// Unsigned operations
System.out.println("\n=== Unsigned ===");
int negative = -1;
System.out.println("-1 as unsigned: " + Integer.toUnsignedString(negative));
// Character utilities
System.out.println("\n=== Character ===");
char c = ;
System.out.println("isDigit('" + c + "'): " + Character.isDigit(c));
System.out.println("isLetter('" + c + "'): " + Character.isLetter(c));
System.out.println("isUpperCase('" + c + "'): " + Character.isUpperCase(c));
System.out.println("toLowerCase('" + c + "'): " + Character.toLowerCase(c));
}
}
public class Utilities {
public static void main(String[] args) {
// Constants
System.out.println("=== Integer Constants ===");
System.out.println("MAX_VALUE: " + Integer.MAX_VALUE);
System.out.println("MIN_VALUE: " + Integer.MIN_VALUE);
System.out.println("BYTES: " + Integer.BYTES);
// Comparison
int a = ;
int b = ;
System.out.println("\n=== Comparison ===");
System.out.println("max(" + a + ", " + b + "): " + Integer.max(a, b));
System.out.println("min(" + a + ", " + b + "): " + Integer.min(a, b));
System.out.println("compare(" + a + ", " + b + "): " + Integer.compare(a, b));
// Unsigned operations
System.out.println("\n=== Unsigned ===");
int negative = -1;
System.out.println("-1 as unsigned: " + Integer.toUnsignedString(negative));
// Character utilities
System.out.println("\n=== Character ===");
char c = ;
System.out.println("isDigit('" + c + "'): " + Character.isDigit(c));
System.out.println("isLetter('" + c + "'): " + Character.isLetter(c));
System.out.println("isUpperCase('" + c + "'): " + Character.isUpperCase(c));
System.out.println("toLowerCase('" + c + "'): " + Character.toLowerCase(c));
}
}
public class Utilities {
public static void main(String[] args) {
// Constants
System.out.println("=== Integer Constants ===");
System.out.println("MAX_VALUE: " + Integer.MAX_VALUE);
System.out.println("MIN_VALUE: " + Integer.MIN_VALUE);
System.out.println("BYTES: " + Integer.BYTES);
// Comparison
int a = ;
int b = ;
System.out.println("\n=== Comparison ===");
System.out.println("max(" + a + ", " + b + "): " + Integer.max(a, b));
System.out.println("min(" + a + ", " + b + "): " + Integer.min(a, b));
System.out.println("compare(" + a + ", " + b + "): " + Integer.compare(a, b));
// Unsigned operations
System.out.println("\n=== Unsigned ===");
int negative = -1;
System.out.println("-1 as unsigned: " + Integer.toUnsignedString(negative));
// Character utilities
System.out.println("\n=== Character ===");
char c = ;
System.out.println("isDigit('" + c + "'): " + Character.isDigit(c));
System.out.println("isLetter('" + c + "'): " + Character.isLetter(c));
System.out.println("isUpperCase('" + c + "'): " + Character.isUpperCase(c));
System.out.println("toLowerCase('" + c + "'): " + Character.toLowerCase(c));
}
}
max(), min(), compare(), constants like MAX_VALUE - all from wrappers.
Exercise: AllWrappers.java
Explore all wrapper classes: Boolean, Byte, Short, Integer, Long, Float, Double, Character