Android road 22 -- Java foundation 16

Guide reading

1. Comparison between packaging type and basic data type
2. Introduction to packaging and common methods
3. Packing and unpacking
4. Conversion between basic types and strings
5. Supplement

Comparison between packaging type and basic data type

Brief introduction of packaging and common methods

Boxing and UnBoxing

package Warpped;

public class WarpTest {

    public static void main(String[] args){
        //Boxing
        //1. Automatic packing
        int t1=2;
        Integer t2=t1;
        //2. Manual packing
        Integer t3=new Integer(t1);
        Integer t4=new Integer("2");
        //test
        System.out.println("t1="+t1);
        System.out.println("t2="+t2);
        System.out.println("t3="+t3);
        System.out.println("t4="+t4);

        //Unpacking
        //1. Automatic unpacking
        int t5=t2;
        //2. Manual unpacking
        int t6=t2.intValue();
        System.out.println("t5="+t5);
        System.out.println("t6="+t6);
    }

}

Output results
t1=2
t2=2
t3=2
t4=2
t5=2
t6=2

Conversion between basic types and strings

package Warpped;

public class WarpTest {

    public static void main(String[] args){
        //Convert basic data type to string
        int t1=2;
        String t2=Integer.toString(t1);
        //String to basic data type
        //Method 1: parse of the wrapper class
        int t3=Integer.parseInt(t2);
        //Method 2: valueOf of the packing class, first convert the string to the packing type, and then convert it to the basic type through automatic unpacking
        int t4=Integer.valueOf(t2);
        //Test:
        System.out.println("Basic data to character t2="+t2);
        System.out.println("Method one of character to basic data t3="+t3);
        System.out.println("Method 2 of character to basic data t4="+t4);
    }

}

Output results
Basic data conversion character t2=2
Character to basic data method 1 t3=2
Character to basic data method 2 t4=2

supplement

A kind of The initial value of the packing type is null by default

package Warpped;

public class WarpTest {

    public static void main(String[] args){
        Integer one=new Integer(100);
        Integer two=new Integer(100);
        System.out.println("one And two Compare:"+(one==two));
        //Because one and two correspond to two different spaces
        Integer three=100;
        System.out.println("three Compare with 100:"+(three==100));
        //three automatically unpacks when comparing
        Integer four=100;
        System.out.println("three And four Compare:"+(three==four));
        //See the figure below
        Integer five=200;
        System.out.println("five Compare with 200:"+(five==200));
        //five automatically unpacks when comparing
        Integer six=200;
        System.out.println("five And six Compare:"+(five==six));
        //See the figure below
    }

}

Output results

one compared with two: false
three compared to 100: true
Compare three with four: true
Compare five with 200: true
Compare five with six: false

A kind of : ecplise displays the setting of the programmed line number
Preferences - > General - > editors - > text editors

Posted by dirkdetken on Sat, 02 May 2020 13:45:43 -0700