Home  >  Article  >  Java  >  How to use Wrapper in Java

How to use Wrapper in Java

WBOY
WBOYforward
2023-05-02 19:07:042008browse
    ##1. Overview

    Define corresponding reference types - packaging classes (packaging classes) for eight basic data types.

    2. Function

    With the characteristics of a class, you can call methods in the class. Java is truly object-oriented.

    The corresponding relationship between the eight basic data types and their packaging classes:

    Basic data typesPackaging classes##byteByteshortShortintIntegerlongLong##float##doubleboolean1. Basic data type-Instance of packaging class (boxing)
    Float
    Double
    Boolean ##char
    Character 3. Use
    ⭕ Implemented through the constructor of the packaging class:

    int i = 500; 
    Integer t = new Integer(i);

    ⭕ Constructing the packaging class object through string parameters:

    Float f = new Float(“4.56”);
    //Long l = new Long(“asdf”); 报错:NumberFormatException

    ⭕ Error example:

    Integer in3 = new Integer("123abc");
    System.out.println(in3.toString());
    /*  
    报错:
    Exception in thread "main" java.lang.NumberFormatException: For input string: "123abc"
     */

    2. Packaging class - basic Data type (unboxing)

    ⭕ Call the

    xxxValue()

    method of the packaging class:

    boolean b = bObj.booleanValue();

    Note: After

    JDK1.5, automatic support is supported Packing, automatic unboxing. But the type must match

    3. String-Basic data type/wrapper class

    ⭕ Implemented through the constructor of the wrapper class:

        int i = new Integer(“12”);

    ⭕ Through the

    of the wrapper class parseXxx(String s)

    Static method:

    Float f = Float.parseFloat(“12.1”);

    ⭕ Error example:

    int num1 = (int)str1;
    Integer in1 = (Integer)str1;
    //可能会报NumberFormatException
    4. Basic data type/packaging class-String

    ⭕ Call string heavy Loaded

    valueOf()

    method:

       String fstr = String.valueOf(2.34f);

    ⭕ More direct way:

       String intStr = 5 + "";
    5. Conversion graph

    四、Classic questions

    //(1)
         Object o1 = true ? new Integer(1) : new Double(2.0);
         System.out.println(o1);//1.0
         Object o2;
            if (true)
                o2 = new Integer(1);
            else
                o2 = new Double(2.0);
            System.out.println(o2);//1
        }
    //(2)
    //java
            Integer i = new Integer(1);
            Integer j = new Integer(1);
            System.out.println(i == j);//false
            Integer m = 1;
            Integer n = 1;
            System.out.println(m == n);//true
            Integer x = 128;
            Integer y = 128;
            System.out.println(x == y);//false

    The above is the detailed content of How to use Wrapper in Java. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete