Wrapper

In Java, a wrapper class provides a way to wrap primitive data types into objects. It allows you to use the primitive data types as objects, enabling you to access their associated methods and utilize them in a more flexible manner. The wrapper classes are part of the java.lang package and provide classes for each of the primitive data types.

Here's an example that demonstrates how to use a wrapper class in Java:

___________________________________________________________________________________

public class WrapperExample {

    public static void main(String[] args) {

        // Creating a wrapper object using the Integer wrapper class
        Integer myInteger = new Integer(42);

        // Accessing the value using wrapper object
        int value = myInteger.intValue();
        System.out.println("Value: " + value);

        // Using wrapper class methods
        String binaryString = myInteger.toBinaryString(42);
        System.out.println("Binary: " + binaryString);

        // Autoboxing: Converting primitive to wrapper automatically
        Integer autoBoxed = 10;
        System.out.println("Autoboxed: " + autoBoxed);

        // Unboxing: Converting wrapper to primitive automatically
        int unboxed = autoBoxed;
        System.out.println("Unboxed: " + unboxed);
    }
}
__________________________________________________________________________

In this example, we create an object myInteger of the Integer wrapper class, which wraps the primitive integer value 42. We can then access the value using the intValue() method of the wrapper class. Additionally, we use the toBinaryString() method of the Integer class to convert the value to its binary representation.

The example also demonstrates autoboxing and unboxing. Autoboxing allows you to assign a primitive value directly to a wrapper object, and unboxing automatically converts a wrapper object back to its corresponding primitive value.

Wrapper classes in Java provide various methods and functionalities to work with the respective primitive data types. Some commonly used wrapper classes include Integer, Double, Boolean, Character, etc. They offer methods to convert values, perform arithmetic operations, compare values, and more.

Using wrapper classes can be beneficial when you need to work with primitive types in a more object-oriented way or when you require additional functionality that is not available with primitive types alone.


Wrapper classes in Java provide various methods and functionalities to work with the respective primitive data types. Here's an overview of the commonly used wrapper classes and some of their methods:

Integer class:

>intValue(): Returns the value of the Integer object as an int.
>parseInt(String s): Parses the string argument as a signed decimal integer and returns an       Integer object.
>compareTo(Integer anotherInteger): Compares two Integer objects numerically.
>toBinaryString(int i): Returns a string representation of the integer argument as an >unsigned integer in base 2 (binary) format.
>valueOf(int i): Returns an Integer object representing the specified int value.

Double class:

>doubleValue(): Returns the value of the Double object as a double.
>parseDouble(String s): Parses the string argument as a double and returns a Double object.
>compareTo(Double anotherDouble): Compares two Double objects numerically.
>toHexString(double d): Returns a hexadecimal string representation of the double argument.
>valueOf(double d): Returns a Double object representing the specified double value.

Boolean class:

>booleanValue(): Returns the value of the Boolean object as a boolean.
>parseBoolean(String s): Parses the string argument as a boolean value.
>compareTo(Boolean anotherBoolean): Compares two Boolean objects.
>toString(boolean b): Returns a String object representing the specified boolean.

Character class:

>charValue(): Returns the value of the Character object as a char.
>isDigit(char ch): Determines if the specified character is a digit.
>isLetter(char ch): Determines if the specified character is a letter.
>isUpperCase(char ch): Determines if the specified character is an uppercase letter.
>toLowerCase(char ch): Converts the character argument to lowercase.

These are just a few examples of the methods provided by the wrapper classes. Each wrapper class offers additional methods to perform various operations and conversions related to their respective primitive types. You can refer to the Java documentation for the complete list of methods available for each wrapper class.


Here are examples demonstrating the usage of methods from each of the wrapper classes in Java:

Integer class:

Integer myInteger = new Integer(42);
int value = myInteger.intValue();
System.out.println("Value: " + value);

*********************************************************
String binaryString = Integer.toBinaryString(42);
System.out.println("Binary: " + binaryString);

**********************************************************
Integer parsedInteger = Integer.parseInt("100");
System.out.println("Parsed Integer: " + parsedInteger);

**********************************************************
int comparison = myInteger.compareTo(parsedInteger);
System.out.println("Comparison: " + comparison);

**********************************************************
Integer valueOfInteger = Integer.valueOf(10);
System.out.println("ValueOf Integer: " + valueOfInteger);


Double class:

Double myDouble = new Double(3.14);
double value = myDouble.doubleValue();
System.out.println("Value: " + value);


**********************************************************
String hexString = Double.toHexString(3.14);
System.out.println("Hexadecimal: " + hexString);


**********************************************************
Double parsedDouble = Double.parseDouble("2.718");
System.out.println("Parsed Double: " + parsedDouble);


**********************************************************
int comparison = myDouble.compareTo(parsedDouble);
System.out.println("Comparison: " + comparison);


**********************************************************
Double valueOfDouble = Double.valueOf(1.414);
System.out.println("ValueOf Double: " + valueOfDouble);


Boolean class:

Boolean myBoolean = new Boolean(true);
boolean value = myBoolean.booleanValue();
System.out.println("Value: " + value);

**********************************************************

**********************************************************
boolean parsedBoolean = Boolean.parseBoolean("false");
System.out.println("Parsed Boolean: " + parsedBoolean);


**********************************************************
int comparison = myBoolean.compareTo(parsedBoolean);
System.out.println("Comparison: " + comparison);


**********************************************************
String stringBoolean = Boolean.toString(true);
System.out.println("String Boolean: " + stringBoolean);

Character class:

Character myChar = new Character('A');
char value = myChar.charValue();
System.out.println("Value: " + value);


**********************************************************
boolean isDigit = Character.isDigit('7');
System.out.println("Is Digit: " + isDigit);


**********************************************************
boolean isLetter = Character.isLetter('X');
System.out.println("Is Letter: " + isLetter);


**********************************************************
boolean isUpperCase = Character.isUpperCase('c');
System.out.println("Is Uppercase: " + isUpperCase);


**********************************************************
char toLowerCase = Character.toLowerCase('F');
System.out.println("Lowercase: " + toLowerCase);

Comments

Popular posts from this blog

Conditional Statement

DataType and Variable