#javaDay30
Geeks With Geeks Geeks With Geeks
548 subscribers
5 views
0

 Published On Jun 7, 2024

In Java, wrapper classes are used to convert primitive data types into objects. This is useful in situations where primitives need to be treated as objects, such as when working with Java Collections (which require objects rather than primitives) or when using certain APIs that only accept objects.

Primitive Data Types and Their Wrapper Classes
Each primitive data type in Java has a corresponding wrapper class:
- `byte` - `Byte`
- `short` - `Short`
- `int` - `Integer`
- `long` - `Long`
- `float` - `Float`
- `double` - `Double`
- `char` - `Character`
- `boolean` - `Boolean`

Autoboxing and Unboxing
Java provides a feature called autoboxing to automatically convert a primitive to its corresponding wrapper class. Conversely, unboxing is the automatic conversion of a wrapper class to its corresponding primitive type.

#### Example:
```java
public class WrapperExample {
public static void main(String[] args) {
// Autoboxing: converting a primitive type to its corresponding wrapper class
int primitiveInt = 5;
Integer wrapperInt = primitiveInt; // Integer.valueOf(primitiveInt) is called behind the scenes

// Unboxing: converting a wrapper class to its corresponding primitive type
Integer anotherWrapperInt = new Integer(10);
int anotherPrimitiveInt = anotherWrapperInt; // anotherWrapperInt.intValue() is called behind the scenes

System.out.println("Wrapper Integer: " + wrapperInt); // Output: Wrapper Integer: 5
System.out.println("Primitive int: " + anotherPrimitiveInt); // Output: Primitive int: 10
}
}
```

Useful Methods in Wrapper Classes
Wrapper classes provide various useful methods for converting between types, parsing strings, etc.

#### Example:
```java
public class WrapperMethodsExample {
public static void main(String[] args) {
// Parsing a string to a primitive type
int parsedInt = Integer.parseInt("123");
double parsedDouble = Double.parseDouble("3.14");

// Converting a wrapper class to a string
Integer wrapperInt = 456;
String intAsString = wrapperInt.toString();

// Getting the primitive value from a wrapper class
Integer anotherWrapperInt = new Integer(789);
int primitiveInt = anotherWrapperInt.intValue();

System.out.println("Parsed int: " + parsedInt); // Output: Parsed int: 123
System.out.println("Parsed double: " + parsedDouble); // Output: Parsed double: 3.14
System.out.println("Integer as String: " + intAsString); // Output: Integer as String: 456
System.out.println("Primitive int: " + primitiveInt); // Output: Primitive int: 789
}
}
```

Advantages of Wrapper Classes
1. **Collection Framework**: Collections in Java, such as `ArrayList`, `HashMap`, etc., only work with objects, so wrapper classes allow primitives to be used with these collections.
2. **Utility Methods**: Wrapper classes provide many utility methods that are not available with primitive types.
3. **Synchronization**: Wrapper classes can be used in multi-threaded environments where synchronization is required since they are objects.
4. **Null Values**: Wrapper classes can be `null`, whereas primitive types cannot. This can be useful in certain situations, such as when dealing with databases where a `null` value might be meaningful.

Limitations
1. **Performance Overhead**: Using wrapper classes introduces a performance overhead due to object creation and garbage collection.
2. **Increased Memory Usage**: Wrapper classes consume more memory than primitive types because they are objects with additional metadata.

show more

Share/Embed