Home > Java > javaTutorial > body text

How to use Wrapper in Java

WBOY
Release: 2023-05-02 19:07:04
forward
2037 people have browsed it
    ##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);
    Copy after login

    ⭕ Constructing the packaging class object through string parameters:

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

    ⭕ Error example:

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

    2. Packaging class - basic Data type (unboxing)

    ⭕ Call the

    xxxValue()

    method of the packaging class:

    boolean b = bObj.booleanValue();
    Copy after login

    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”);
    Copy after login

    ⭕ Through the

    of the wrapper class parseXxx(String s)

    Static method:

    Float f = Float.parseFloat(“12.1”);
    Copy after login

    ⭕ Error example:

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

    ⭕ Call string heavy Loaded

    valueOf()

    method:

       String fstr = String.valueOf(2.34f);
    Copy after login

    ⭕ More direct way:

       String intStr = 5 + "";
    Copy after login
    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
    Copy after login

    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!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template