Wrapper Classes in Java ?

 ### Wrapper Classes in Java


Wrapper classes in Java provide a way to use primitive data types (int, char, etc.) as objects. Each primitive data type has a corresponding wrapper class in the `java.lang` package:


| Primitive Type | Wrapper Class  |

| -------------- | -------------- |

| `byte`         | `Byte`         |

| `short`        | `Short`        |

| `int`          | `Integer`      |

| `long`         | `Long`         |

| `float`        | `Float`        |

| `double`       | `Double`       |

| `char`         | `Character`    |

| `boolean`      | `Boolean`      |


### Key Features of Wrapper Classes


1. **Autoboxing and Unboxing**: 

   - **Autoboxing**: Automatic conversion of primitive types to their corresponding wrapper classes.

     ```java

     int num = 5;

     Integer obj = num; // Autoboxing

     ```

   - **Unboxing**: Automatic conversion of wrapper class objects to their corresponding primitive types.

     ```java

     Integer obj = new Integer(5);

     int num = obj; // Unboxing

     ```


2. **Immutability**: Wrapper class objects are immutable, meaning their values cannot be changed once created.


3. **Utility Methods**: Wrapper classes provide several utility methods for converting between different types, parsing strings, and more.


### Methods of Wrapper Classes


Below are some commonly used methods of each wrapper class:


#### 1. `Byte` Class

- **Constructors**:

  ```java

  Byte(byte value)

  Byte(String s)

  ```

- **Methods**:

  ```java

  byte byteValue()          // Returns the value as a byte

  int compareTo(Byte b)     // Compares two Byte objects

  static Byte valueOf(byte b)  // Returns a Byte instance representing the specified byte value

  static Byte valueOf(String s)  // Returns a Byte instance representing the specified String value

  static byte parseByte(String s)  // Parses the string argument as a signed byte

  ```


#### 2. `Short` Class

- **Constructors**:

  ```java

  Short(short value)

  Short(String s)

  ```

- **Methods**:

  ```java

  short shortValue()         // Returns the value as a short

  int compareTo(Short s)     // Compares two Short objects

  static Short valueOf(short s) // Returns a Short instance representing the specified short value

  static Short valueOf(String s) // Returns a Short instance representing the specified String value

  static short parseShort(String s) // Parses the string argument as a signed short

  ```


#### 3. `Integer` Class

- **Constructors**:

  ```java

  Integer(int value)

  Integer(String s)

  ```

- **Methods**:

  ```java

  int intValue()                            // Returns the value as an int

  int compareTo(Integer i)          // Compares two Integer objects

  static Integer valueOf(int i)      // Returns an Integer instance representing the specified int value

  static Integer valueOf(String s)     // Returns an Integer instance representing the specified String value

  static int parseInt(String s)  // Parses the string argument as a signed decimal integer

  static int parseInt(String s, int radix) // Parses the string argument as a signed integer in the specified radix

  static String toString(int i)  // Returns a String object representing the specified integer

  ```


#### 4. `Long` Class

- **Constructors**:

  ```java

  Long(long value)

  Long(String s)

  ```

- **Methods**:

  ```java

  long longValue()           // Returns the value as a long

  int compareTo(Long l)      // Compares two Long objects

  static Long valueOf(long l)  // Returns a Long instance representing the specified long value

  static Long valueOf(String s)  // Returns a Long instance representing the specified String value

  static long parseLong(String s)  // Parses the string argument as a signed decimal long

  static long parseLong(String s, int radix) // Parses the string argument as a signed long in the specified radix

  static String toString(long l)  // Returns a String object representing the specified long

  ```


#### 5. `Float` Class

- **Constructors**:

  ```java

  Float(float value)

  Float(String s)

  ```

- **Methods**:

  ```java

  float floatValue()         // Returns the value as a float

  int compareTo(Float f)     // Compares two Float objects

  static Float valueOf(float f) // Returns a Float instance representing the specified float value

  static Float valueOf(String s) // Returns a Float instance representing the specified String value

  static float parseFloat(String s) // Parses the string argument as a float

  static String toString(float f)  // Returns a String object representing the specified float

  ```


#### 6. `Double` Class

- **Constructors**:

  ```java

  Double(double value)

  Double(String s)

  ```

- **Methods**:

  ```java

  double doubleValue()       // Returns the value as a double

  int compareTo(Double d)    // Compares two Double objects

  static Double valueOf(double d) // Returns a Double instance representing the specified double value

  static Double valueOf(String s) // Returns a Double instance representing the specified String value

  static double parseDouble(String s) // Parses the string argument as a double

  static String toString(double d)  // Returns a String object representing the specified double

  ```


#### 7. `Character` Class

- **Constructors**:

  ```java

  Character(char value)

  ```

- **Methods**:

  ```java

  char charValue()          // Returns the value as a char

  int compareTo(Character c) // Compares two Character objects

  static Character valueOf(char c) // Returns a Character instance representing the specified char value

  static boolean isLetter(char c)  // Determines if the specified char value is a letter

  static boolean isDigit(char c)  // Determines if the specified char value is a digit

  static char toUpperCase(char c)  // Converts the specified char value to uppercase

  static char toLowerCase(char c)  // Converts the specified char value to lowercase

  ```


#### 8. `Boolean` Class

- **Constructors**:

  ```java

  Boolean(boolean value)

  Boolean(String s)

  ```

- **Methods**:

  ```java

  boolean booleanValue()     // Returns the value as a boolean

  int compareTo(Boolean b)   // Compares two Boolean objects

  static Boolean valueOf(boolean b)  // Returns a Boolean instance representing the specified boolean value

  static Boolean valueOf(String s)  // Returns a Boolean instance representing the specified String value

  static boolean parseBoolean(String s) // Parses the string argument as a boolean

  static String toString(boolean b)  // Returns a String object representing the specified boolean

  ```


### Example Usage of Wrapper Classes


```java

public class WrapperExample {

    public static void main(String[] args) {

        // Autoboxing

        Integer intObj = 10;

        Double doubleObj = 15.5;

        Character charObj = 'a';


        // Unboxing

        int intVal = intObj;

        double doubleVal = doubleObj;

        char charVal = charObj;


        // Using utility methods

        int parsedInt = Integer.parseInt("123");

        double parsedDouble = Double.parseDouble("123.45");

        boolean parsedBoolean = Boolean.parseBoolean("true");


        System.out.println("Autoboxed values: " + intObj + ", " + doubleObj + ", " + charObj);

        System.out.println("Unboxed values: " + intVal + ", " + doubleVal + ", " + charVal);

        System.out.println("Parsed values: " + parsedInt + ", " + parsedDouble + ", " + parsedBoolean);

    }

}

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

Best Example : 

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

/*    EX 1 :

- **Autoboxing**: Primitive value ko wrapper class object mein automatically convert kiya jaata hai. */

  

  int primitiveValue = 10;       // primitive value

  Integer objectValue = primitiveValue;  // Autoboxing - primitive se object mein value assign ho rahi hai

  /* 


- **Auto-unboxing**: Wrapper class object ko primitive value mein automatically convert kiya jaata hai.*/


  Integer objectValue = 10;     // wrapper class object

  int primitiveValue = objectValue;  // Auto-unboxing - object se primitive value mein data ja raha hai

  

  // Ex 2 :


public class Integer_Class {

    public static void main(String[] args) {


        // Auto Boxing

        // It means Integer n=new Integer(10); ( Automatically Create ho rha hai Object)

        // primitive object mein directlt value assign ho rha hai

        Integer n = 10;


        // Auto Unboxing

        // It means int n1=n.intvalue(); (Automatically object ka data primitive value

        // mein jaa rha hai)

        // Primitive value mein object ka data jaa rha hai

        int n1 = n;


        System.out.println(n1);


    }

}

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

### Summary


Wrapper classes in Java provide a way to treat primitive data types as objects, allowing for more flexibility and utility methods for parsing, comparing, and converting data. They are essential for scenarios where objects are needed instead of primitives, such as in collections like `ArrayList`.

Comments

Popular posts from this blog

Collection Framework of Java

What is DBMS ?

Compiler vs Interpreter vs JIT Compiler