Java Wrapper Classes are a set of classes that provide a way to use primitive data types (such as int, float, boolean) as objects in Java. Every primitive data type has a corresponding wrapper class in Java, which provides several utility methods and allows the primitive data types to be used in situations where objects are required.
Here are the primitive data types and their corresponding wrapper classes in Java:
- byte – Byte
- short – Short
- int – Integer
- long – Long
- float – Float
- double – Double
- boolean – Boolean
- char – Character
Here are some examples of how to use Java wrapper classes:
Integer Wrapper Class
// Creating an Integer object Integer num = new Integer(10); // Converting an int to an Integer object Integer anotherNum = Integer.valueOf(20); // Converting a String to an Integer object Integer yetAnotherNum = Integer.parseInt("30"); // Using Integer class methods int sum = num.intValue() + anotherNum.intValue(); System.out.println("Sum of " + num + " and " + anotherNum + " is " + sum);
Boolean Wrapper Class:
// Creating a Boolean object Boolean flag = new Boolean(true); // Converting a String to a Boolean object Boolean anotherFlag = Boolean.valueOf("false"); // Using Boolean class methods if (flag.booleanValue() && !anotherFlag.booleanValue()) { System.out.println("Flag is true and anotherFlag is false."); }
Double Wrapper Class:
// Creating a Double object Double num = new Double(3.14159); // Converting a String to a Double object Double anotherNum = Double.parseDouble("2.71828"); // Using Double class methods double product = num.doubleValue() * anotherNum.doubleValue(); System.out.println("Product of " + num + " and " + anotherNum + " is " + product);
Character Wrapper Class:
// Creating a Character object Character letter = new Character('A'); // Using Character class methods System.out.println("The letter " + letter + " is uppercase: " + Character.isUpperCase(letter)); System.out.println("The letter " + letter + " is lowercase: " + Character.isLowerCase(letter));
Long Wrapper Class:
// Creating a Long object Long num = new Long(1234567890); // Converting a String to a Long object Long anotherNum = Long.valueOf("9876543210"); // Using Long class methods long difference = num.longValue() - anotherNum.longValue(); System.out.println("Difference between " + num + " and " + anotherNum + " is " + difference);
Wrapper classes are commonly used when working with collections, such as ArrayLists or HashMaps, which require objects to be stored rather than primitive data types. The wrapper classes also provide a way to convert primitive data types to and from strings.
Wrapper classes are immutable, meaning that once an object is created, its value cannot be changed. However, they provide several utility methods, such as parseXXX() methods for converting strings to primitive data types and valueOf() methods for converting primitive data types to wrapper class objects.
Wrapper classes are part of the Java API and are located in the java.lang package. They provide a convenient way to work with primitive data types as objects in Java programs.