4.7 Wrapper Classes

0 阅读2分钟

1. Exam Points

  • If a method has a parameter of int or Integer, you can pass into an int value or an Integer value.
  • If a method has a parameter of double or Double, you can pass into a double value or a Double value.
  • Autoboxing is done automatically: int -> Integer, double -> Double
  • Unboxing is done automatically: Integer -> int, Double -> double
  • String to int or double:
    • Integer.parseInt(String x): String to int
    • Double.parseDouble(String x): String to double
  • You can perform arithmetic operations on wrapper objects.

2. Knowledge Points

(1) Wrapper Classes

  • You can not create objects of primitive data types (int, double, boolean).
    image.png
  • To convert primitive values to objects, Java provides wrapper classes.
  • The wrapper classes are from the java.lang package.
  • Wrapper classes for primitive data types:
    • int -> Integer
    • double -> Double
    • boolean -> Boolean

(2) Autoboxing and Unboxing

  • Autoboxing (自动装箱) is the automatic conversion from primitive types to the corresponding object wrapper classes. (primitive types -> wrapper classes)

    • Ex. int to Integer, double to Double, boolean to Boolean.
    • Example:
      int a = 10; // primitive
      Integer b = a; // int -> Integer
      
      double d = 2.3;
      Double e = d; // double -> Double
      
    • The Java compiler applies autoboxing when a primitive value is:
      • passed as a parameter to a method that expects an object of the corresponding wrapper class.
      public static void printSomething(Integer x) {
                  System.out.println(x);
      }
      
      public static void main(String[] args) {
          int a = 10;
          printSomething(a); // int(a) -> Integer(x);
      }
      
      • assigned to a variable of the corresponding wrapper class.
      int a = 10; // primitive
      Integer b = a; // int -> Integer
      
    • You can perform arithmetic operations on Wrapper objects:
      double x = 10.0;
      Double y = 20.0;
      Double z = 30.0;
      System.out.println(x + y + z); // 60.0 is printed
      
    • In summary:
      • use an Integer value like an int value, like in arithmetic operations.
      • if a parameter is of int type, you can pass an argument of either int or Integer.
  • Unboxing (拆箱) is the automatic conversion from a wrapper class to its primitive type. (wrapper classes -> primitive types)

    • Ex. Integer to int, Double to double, Boolean to boolean
    • Example:
    Integer a = new Integer(10);
    int b = a;  //Integer -> int
    
    Double a = new Double(2.3);
    double b = a; //Double -> double
    
    • The Java compiler applies unboxing when a wrapper class object is:
      • passed as a parameter to a method that expects a value of the corresponding primitive type.
      public static void printSomething(int x) {
                  System.out.println(x);
      }
      
      public static void main(String[] args) {
          Integer a = new Integer(10);
          printSomething(a); // Integer(a) -> int(x);
      }
      
      • assigned to a variable of the corresponding primitive type.
      Integer a = new Integer(10);
      int b = a;  //Integer -> int
      
  • If a parameter is of Integer type, you can pass an argument of either int or Integer.

(3) Convert String Values

  • static int parseInt(String s) : a method of the Integer class, and is used to convert a String value to an int value.
    • Example:
    String a = "123";
    String b = "abc";
    	
    // parseInt() is a static method, call it by the class name Integer
    int x = Integer.parseInt(a); // String -> int
    int y = Integer.parseInt(b); // error, abc can not be converted to an int value
    
  • static double parseDouble(String s) : a method of the Double class, and is used to convert a String value to a double value.
    • Example:
    String a = "23.6";
    // parseDouble() is a static method, call it by the class name Double
    double x = Double.parseDouble(a);
    

3. Exercises