Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
Wrapper classes in Java provide a way to use primitive data types as objects. The Java standard library provides a wrapper class for each of the eight primitive data types:
These wrapper classes are part of the java.lang
package and are automatically imported.
Boxing and Unboxing
Boxing is the process of converting a primitive data type to its corresponding wrapper class object. Unboxing is the reverse process: converting a wrapper class object back to its corresponding primitive data type.
Java automatically handles boxing and unboxing, allowing you to use primitive data types and wrapper class objects interchangeably in most cases. This feature is called autoboxing and auto-unboxing.
Boxing Example:
int primitiveInt = 42; Integer wrappedInt = primitiveInt; // Autoboxing: int to Integer
Unboxing Example:
Integer wrappedInt = new Integer(42); int primitiveInt = wrappedInt; // Auto-unboxing: Integer to int
Wrapper Class Usage
Wrapper classes are useful in situations where you need to use a primitive data type as an object. Some common scenarios include:
Example:
import java.util.ArrayList; public class WrapperClassExample { public static void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<>(); // Autoboxing: Adding primitive int values to ArrayList<Integer> numbers.add(10); numbers.add(20); numbers.add(30); // Auto-unboxing: Retrieving primitive int values from ArrayList<Integer> int sum = numbers.get(0) + numbers.get(1) + numbers.get(2); System.out.println("Sum: " + sum); // Output: Sum: 60 // Using utility method from wrapper class String numberString = "42"; int parsedInt = Integer.parseInt(numberString); System.out.println("Parsed int: " + parsedInt); // Output: Parsed int: 42 } }
In conclusion, wrapper classes in Java allow you to use primitive data types as objects, providing additional functionality and flexibility. The process of converting primitive data types to wrapper class objects is called boxing, while the reverse process is called unboxing. Java automatically handles boxing and unboxing through autoboxing and auto-unboxing, making it easy to work with both primitive data types and their corresponding wrapper classes.
Boxing and unboxing in Java with examples: Boxing is the process of converting a primitive type to its corresponding wrapper class, while unboxing is the reverse.
// Boxing int primitiveInt = 42; Integer wrapperInt = Integer.valueOf(primitiveInt); // Unboxing int unboxedInt = wrapperInt.intValue();
Converting primitive types to wrapper classes in Java: Converting primitive types to their wrapper classes can be done explicitly or implicitly (autoboxing).
// Explicit conversion int intValue = 42; Integer wrapperInt = Integer.valueOf(intValue); // Autoboxing (implicit conversion) int anotherValue = 84; Integer autoboxedInt = anotherValue;
Java autoboxing and autounboxing: Autoboxing and autounboxing refer to automatic conversion between primitive types and their corresponding wrapper classes.
// Autoboxing int intValue = 42; Integer wrapperInt = intValue; // Autounboxing int unboxedInt = wrapperInt;
Wrapper classes and collections in Java: Wrapper classes are often used in collections, as collections typically work with objects, not primitives.
List<Integer> integerList = new ArrayList<>(); integerList.add(1); integerList.add(2);
Using wrapper classes for null values in Java: Wrapper classes allow for the representation of null values, unlike primitive types.
Integer nullableValue = null;
Parsing and formatting with wrapper classes in Java: Wrapper classes provide methods for parsing and formatting their respective primitive types.
String numberStr = "123"; int parsedInt = Integer.parseInt(numberStr);
Working with wrapper classes in Java streams: Wrapper classes are often used in stream operations, especially when dealing with collections.
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); int sum = numbers.stream().mapToInt(Integer::intValue).sum();
Java Wrapper Classes and type conversion: Wrapper classes facilitate type conversion between primitive types and objects.
Integer wrapperInt = 42; double convertedDouble = wrapperInt.doubleValue();
Handling null values with wrapper classes in Java: Wrapper classes allow null values, which can be useful for representing the absence of a value.
Integer nullableInt = null;
Converting wrapper classes to primitive types in Java: Convert wrapper classes to their corresponding primitive types explicitly.
Integer wrapperInt = 42; int primitiveInt = wrapperInt.intValue();
Java Wrapper Classes and equality comparisons:
Be cautious when comparing wrapper classes using ==
as it compares references, not values. Use equals()
for value comparison.
Integer a = 42; Integer b = 42; // Incorrect: Compares references if (a == b) { // Code } // Correct: Compares values if (a.equals(b)) { // Code }