Skip to content

Java Wrapper classes ‚Äč

They wrap around primitive data types, adding extra functionality and flexibility to our code. They allow us to treat primitive types as objects, unlocking a world of possibilities in our Java programs.

Some common wrapper classes in Java:

  1. Integer: Wraps around the int primitive data type, allowing us to perform operations such as converting strings to integers or comparing numbers more conveniently.

  2. Double: Wraps around the double primitive data type, enabling us to work with decimal numbers and perform mathematical operations with precision.

  3. Boolean: Wraps around the boolean primitive data type, providing us with methods to manipulate boolean values and perform logical operations.

  4. Character: Wraps around the char primitive data type, allowing us to work with individual characters and perform character-based operations.

These wrapper classes act as bridges between the world of primitives and the world of objects, making our code more versatile and expressive. They offer methods to convert between primitive types and objects, as well as additional functionality for manipulating and working with data.

Let's delve into a simple example to see wrapper classes in action:

java
public class WrapperClassExample {
    public static void main(String[] args) {
        Integer num1 = 10; // Using Integer wrapper class
        Integer num2 = 20;

        int sum = num1 + num2; // We can directly use arithmetic operations on Integer objects
        System.out.println("Sum of num1 and num2: " + sum);
    }
}

In this example, we're using the Integer wrapper class to wrap around primitive integers num1 and num2. We can then perform arithmetic operations directly on these objects, making our code more readable and concise.

java
public class WrapperClassExample {
    public static void main(String[] args) {
        // Example 1: Using Integer wrapper class
        Integer num1 = new Integer(10);
        Integer num2 = 20;

        int sum = num1.intValue() + num2; // Unwrapping and adding
        System.out.println("Sum of numbers: " + sum);

        // Example 2: Using Character wrapper class
        Character letter = 'A';
        System.out.println("Character: " + letter);

        // Example 3: Using Boolean wrapper class
        Boolean flag = true;
        System.out.println("Boolean flag: " + flag);
    }
}

Exciting, isn't it? In our examples, we've used wrapper classes like Integer, Character, and Boolean to work with primitive data types in a more versatile way. These wrapper classes provide useful methods and functionalities that make it easier to manipulate and interact with data.:

Primitive TypeWrapper Class
bytejava.lang.Byte
shortjava.lang.Short
intjava.lang.Integer
longjava.lang.Long
floatjava.lang.Float
doublejava.lang.Double
charjava.lang.Character
booleanjava.lang.Boolean

These wrapper classes allow primitive data types to be treated as objects, enabling additional functionality such as conversion, manipulation, and usage in contexts that require objects.

Waytojava is designed to make learning easier. We simplify examples for better understanding. We regularly check tutorials, references, and examples to correct errors, but it's important to remember that humans can make mistakes.