Understanding Java Wrapper Classes and Their Usage

使用して回答:GPT-4o-mini によってJava Q&A Hub

質問:

Introduce Java Wrapper Classes and give examples

回答:

Java Wrapper Classes are a set of classes in the Java programming language that allow primitive data types to be treated as objects. Each primitive type has a corresponding wrapper class that encapsulates the primitive value in an object. This is particularly useful when working with Java Collections, which can only store objects, not primitive types.

Here are the eight primitive data types in Java and their corresponding wrapper classes:

Primitive TypeWrapper Class
booleanBoolean
byteByte
charCharacter
shortShort
intInteger
longLong
floatFloat
doubleDouble

Examples of Wrapper Classes

  1. Boolean Wrapper Class

    Boolean boolObj = Boolean.valueOf(true);
    System.out.println("Boolean value: " + boolObj);
    
  2. Byte Wrapper Class

    Byte byteObj = Byte.valueOf((byte) 10);
    System.out.println("Byte value: " + byteObj);
    
  3. Character Wrapper Class

    Character charObj = Character.valueOf('A');
    System.out.println("Character value: " + charObj);
    
  4. Short Wrapper Class

    Short shortObj = Short.valueOf((short) 100);
    System.out.println("Short value: " + shortObj);
    
  5. Integer Wrapper Class

    Integer intObj = Integer.valueOf(123);
    System.out.println("Integer value: " + intObj);
    
  6. Long Wrapper Class

    Long longObj = Long.valueOf(123456789L);
    System.out.println("Long value: " + longObj);
    
  7. Float Wrapper Class

    Float floatObj = Float.valueOf(3.14f);
    System.out.println("Float value: " + floatObj);
    
  8. Double Wrapper Class

    Double doubleObj = Double.valueOf(3.14159);
    System.out.println("Double value: " + doubleObj);
    

Autoboxing and Unboxing

Java also provides a feature called autoboxing and unboxing that automatically converts between primitive types and their corresponding wrapper classes.

  • Autoboxing: Automatic conversion of a primitive type to its corresponding wrapper class.

    int primitiveInt = 10;
    Integer wrappedInt = primitiveInt; // Autoboxing
    System.out.println("Wrapped Integer: " + wrappedInt);
    
  • Unboxing: Automatic conversion of a wrapper class to its corresponding primitive type.

    Integer wrappedInt2 = 20;
    int primitiveInt2 = wrappedInt2; // Unboxing
    System.out.println("Primitive Integer: " + primitiveInt2);
    

Use Cases

  1. Collections: Wrapper classes are essential when using Java Collections (like ArrayList, HashMap, etc.) since they can only store objects.

    List<Integer> intList = new ArrayList<>();
    intList.add(1); // Autoboxing
    intList.add(2);
    System.out.println(intList);
    
  2. Null Values: Wrapper classes can hold null values, which can be useful in scenarios where you need to represent the absence of a value.

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

In summary, Java Wrapper Classes are a powerful feature that enhances the flexibility of the language by allowing primitive types to be treated as objects, enabling their use in various contexts such as collections and utility operations.