All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Wrapper Class In Java Example

Last Updated : Mar 11, 2024

Wrapper Class In Java Example

In this article we will show you the solution of wrapper class in java example, an object representation of each of the primitive data types is provided by a wrapper class in Java.

These wrapper classes are used to transform primitive types into objects, enabling the usage of object-oriented characteristics in relation to primitive data types and allowing them to be considered as objects.

In Java, there are wrapper classes for various primitive data types, including Integer, Float, Double, Boolean, Character, Byte, Short, and Long.

These classes offer practical ways to work with and manipulate the values that are enclosed.

When working with Java collections, wrapper classes are very helpful because collections can only contain objects and not primitive types.

Programming may be made more fluid and flexible by converting primitive values into objects and storing them in collections using wrapper classes.

Now we'll look at a java example of the wrapper class concept.

Step By Step Guide On Wrapper Class In Java Example :-

import java.lang.Character;
import java.lang.Boolean;
import java.lang.Byte;
import java.lang.Short;
import java.lang.Long;
import java.lang.Float;
import java.lang.Double;
import java.lang.Integer;
public class WrapperClassExample {
    public static void main(String[] args) {
        Character charObj = 'A';
        Boolean boolObj = true;
        Byte byteObj = 10;
        Short shortObj = 100;
        Long longObj = 1000L;
        Float floatObj = 10.5f;
        Double doubleObj = 10.123;
        // Accessing values and performing operations
        System.out.println("Character: " + charObj);
        System.out.println("Boolean: " + boolObj);
        System.out.println("Byte: " + byteObj);
        System.out.println("Short: " + shortObj);
        System.out.println("Long: " + longObj);
        System.out.println("Float: " + floatObj);
        System.out.println("Double: " + doubleObj);
        // Wrapper class methods
        int intValue = byteObj.intValue(); // converting Byte to int
        System.out.println("Result: " + intValue);
        double doubleValue = floatObj.doubleValue(); // converting Float to double
        System.out.println("Result: " + doubleValue);
        String binaryString = Integer.toBinaryString(20); // converting int to binary string
        System.out.println("Result: " + binaryString);
        boolean equalsResult = doubleObj.equals(10.123); // comparing Double objects
        System.out.println("Result: " + equalsResult);
        int parsedInt = Integer.parseInt("100"); // parsing string to int
        System.out.println("Result: " + parsedInt);
    }
}
  1. You can see that we wrote java code to display the wrapper class in this example.
  2. The essential wrapper classes from the java.lang package are imported at the beginning of the code, but it's crucial to remember that Java already imports these classes by default.
  3. In order to encapsulate the code logic, we construct a class called WrapperClassExample.
  4. We build and initialise a variety of wrapper objects with values of various primitive kinds within the main procedure.
  5. We make a wrapper object of type Character and set charObj's value to A.
  6. We make a wrapper object of type Boolean and set boolObj's value to true.
  7. ByteObj is given the value 10 and a wrapper object of type Byte is created.
  8. We make a wrapper object of type Short and give shortObj the value 100.
  9. We make a Long wrapper object and give its longObj property the value 1000L.
  10. We make a wrapper object of type Float and give floatObj the value 10.5f.
  11. We make a wrapper object of type Double and give doubleObj the value 10.123.
  12. The values of each wrapper object are then printed using System.out.println() commands, enabling us to confirm their contents.
  13. The next line of code shows how to use the wrapper class method.
  14. To turn the Byte object into an int, we call the intValue() method on byteObj.
  15. To change the Float object into a double, we call the doubleValue() function on floatObj.
  16. The Integer class's toBinaryString() method is used to convert the integer 20 to a binary representation.
  17. To compare doubleObj to the number 10.123, we use the equals() function.
  18. To parse the text 100 and turn it into an int, we use the parseInt() function of the Integer type.

Conclusion :-

Thus, using a java example, we were able to understand the wrapper class concept.

We also discovered that wrapper classes in Java are essential for giving primitive data types an object-oriented representation.

They give us the ability to manipulate and encapsulate primitive values as objects, adding new programming functionalities.

I hope this article on wrapper class in java example helps you and the steps and method mentioned above are easy to follow and implement.