In the world of object-oriented programming, everything is an object. This principle holds true for Java as well, where even primitive data types have their corresponding wrapper classes. These wrapper classes provide a way to use primitive data types in a more flexible manner, especially when dealing with collections and APIs that require objects.
Java provides wrapper classes for each of its primitive data types. The most commonly used wrapper classes are Integer, Double, Character, and Boolean. These classes allow you to treat primitive values as objects, enabling operations like adding them to collections or using them in methods that expect objects.
Understanding wrapper classes is crucial because they play a significant role in Java's type system, especially when working with generics and collections. In this tutorial, we'll explore the Integer, Double, Character, and Boolean wrapper classes, as well as the concepts of autoboxing and unboxing.
The Integer class is a wrapper for the primitive data type int. It provides several useful methods for working with integer values, such as parsing strings to integers and converting integers to strings.
1public class IntegerExample {2public static void main(String[] args) {3// Creating an Integer object4Integer num = new Integer(10);56// Converting Integer to int7int primitiveNum = num.intValue();89// Parsing a string to an integer10String str = "20";11int parsedInt = Integer.parseInt(str);1213System.out.println("Integer Object: " + num);14System.out.println("Primitive int: " + primitiveNum);15System.out.println("Parsed Int from String: " + parsedInt);16};17}
Integer Object: 10 Primitive int: 10 Parsed Int from String: 20
The Double class is a wrapper for the primitive data type double. It provides methods for converting between strings and double values, as well as for performing mathematical operations.
1public class DoubleExample {2public static void main(String[] args) {3// Creating a Double object4Double num = new Double(3.14);56// Converting Double to double7double primitiveNum = num.doubleValue();89// Parsing a string to a double10String str = "2.718";11double parsedDouble = Double.parseDouble(str);1213System.out.println("Double Object: " + num);14System.out.println("Primitive double: " + primitiveNum);15System.out.println("Parsed Double from String: " + parsedDouble);16}17}
Double Object: 3.14 Primitive double: 3.14 Parsed Double from String: 2.718
The Character class is a wrapper for the primitive data type char. It provides methods for checking character properties, such as whether a character is a digit or a letter.
1public class CharacterExample {2public static void main(String[] args) {3// Creating a Character object4Character ch = new Character('A');56// Converting Character to char7char primitiveCh = ch.charValue();89// Checking character properties10boolean isDigit = Character.isDigit(ch);11boolean isLetter = Character.isLetter(ch);1213System.out.println("Character Object: " + ch);14System.out.println("Primitive char: " + primitiveCh);15System.out.println("Is Digit: " + isDigit);16System.out.println("Is Letter: " + isLetter);17}18}
Character Object: A Primitive char: A Is Digit: false Is Letter: true
The Boolean class is a wrapper for the primitive data type boolean. It provides methods for converting between strings and boolean values.
1public class BooleanExample {2public static void main(String[] args) {3// Creating a Boolean object4Boolean bool = new Boolean(true);56// Converting Boolean to boolean7boolean primitiveBool = bool.booleanValue();89// Parsing a string to a boolean10String str = "true";11boolean parsedBoolean = Boolean.parseBoolean(str);1213System.out.println("Boolean Object: " + bool);14System.out.println("Primitive boolean: " + primitiveBool);15System.out.println("Parsed Boolean from String: " + parsedBoolean);16}17}
Boolean Object: true Primitive boolean: true Parsed Boolean from String: true
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer.
Unboxing is the reverse process—converting a wrapper class back to its corresponding primitive type.
1public class AutoboxingExample {2public static void main(String[] args) {3// Autoboxing: int to Integer4Integer num = 10;56// Unboxing: Integer to int7int primitiveNum = num;89System.out.println("Autoboxed Integer: " + num);10System.out.println("Unboxed int: " + primitiveNum);11}12}
Autoboxed Integer: 10 Unboxed int: 10
Let's create a practical example that demonstrates the use of wrapper classes and autoboxing/unboxing. We'll create a simple program that reads user input, stores it in a list, and then processes the data.
1import java.util.ArrayList;2import java.util.List;3import java.util.Scanner;45public class PracticalExample {6public static void main(String[] args) {7Scanner scanner = new Scanner(System.in);8List<Integer> numbers = new ArrayList<>();910System.out.println("Enter integers (type 'done' to finish):");1112while (scanner.hasNext()) {13if (scanner.hasNextInt()) {14int num = scanner.nextInt();15numbers.add(num); // Autoboxing16} else {17String input = scanner.next();18if (input.equalsIgnoreCase("done")) {19break;20}21}22}2324System.out.println("You entered the following numbers:");25for (Integer number : numbers) { // Unboxing26System.out.println(number);27}2829scanner.close();30}31}
$ javac PracticalExample.java$ java PracticalExampleEnter integers (type 'done' to finish):10 20 30 doneYou entered the following numbers:102030
| Wrapper Class | Primitive Type |
|---|---|
| Integer | int |
| Double | double |
| Character | char |
| Boolean | boolean |
In the next topic, we'll explore Java Generics. Generics provide a way to write more flexible and reusable code by allowing you to define classes, interfaces, and methods that operate on objects of various types without specifying their exact types. This will help us understand how wrapper classes fit into the broader context of Java's type system.
Stay tuned for our next tutorial where we'll dive deeper into Generics!