Java learning note 24 (Integer class)

Keywords: Java Session JDK jvm

;

The basic data type wrapper class is introduced here. Integer is the wrapper class of int,

The methods of wrapper classes of other basic data types are almost the same as those of Integer, and one of them can be used for plenary session

Characteristics of basic data type wrapper class: used for conversion between basic data type and string

These classes belong to the core classes of java and do not need import

Method of Integer class:

 

parseInt method

Example:

Change string to basic type

package demo;

public class IntegerDemo {
    public static void main(String[] args) {
        function1();
        function2();
    }

    public static void function1() {
        int i = Integer.parseInt("-12");
        // You can change a string into int type
        System.out.println(i / 2);// -6
    }

    public static void function2() {
        int i = Integer.parseInt("1010", 2);
        // Convert a binary string to decimal int type
        System.out.println(i);// 10
    }
}

 

You can also change a basic type to a string:

package demo;

public class IntegerDemo {
    public static void main(String[] args) {
        function1();
        function2();
        function3();
    }

    public static void function1() {
        int i = 3;
        String string = i + "";
        System.out.println(string + 1);
        // Output string 31 here
    }

    public static void function2() {
        int i = 3;
        // there toString Method is not a method that overrides the parent class
        String string = Integer.toString(i);
        System.out.println(string + 1);
        // Output string 31
    }

    public static void function3() {
        int i = 5;
        String string = Integer.toString(i, 2);
        System.out.println(string);
        // Convert to binary number, output string 101
    }
}

 

The construction method of Integerl class:

Example:

package demo;

public class IntegerDemo {
    public static void main(String[] args) {
        function1();
    }

    public static void function1() {
        Integer integer = new Integer("100");
        int i = integer.intValue();
        // Review here by the way i++and++i The difference between
        // System.out.println(i++);//100
        System.out.println(++i);// 101
    }
}

 

Other methods:

package demo;

public class IntegerDemo {
    public static void main(String[] args) {
        function1();
        function2();
    }

    public static void function1() {
        // Integer Static member variable of class
        System.out.println(Integer.MAX_VALUE);
        System.out.println(Integer.MIN_VALUE);
        System.out.println(Integer.SIZE);
        // Output 2147483647 -2147483648 32
    }

    public static void function2() {
        int i = 666;
        System.out.println(Integer.toBinaryString(i));// 1010011010 of binary string
        System.out.println(Integer.toOctalString(i));// 1232 of octal string
        System.out.println(Integer.toHexString(i));// Hexadecimal 29 a

    }
}

 

 

Features after JDK 1.5: automatic packing and unpacking

Auto packing: basic data type, directly changed to object

Auto unbox: data in object changes back to basic data type

Example:

 

package demo;

public class IntegerDemo {
    public static void main(String[] args) {
        function1();
    }

    public static void function1() {
        Integer integer = 1;
        //It's appropriate to write like this. Automatic packing
        //Essentially: Integer in = new Integer(1)
        integer = integer + 1;
        //Unpack automatically, split reference type into basic type and do operation again
        //Essentially: integer+1 <==> integer.intValue()+1 = 2
        //Reassign to integer Automatic packing
        System.out.println(integer);
        //Print the object, but not the object address, but 1
    }
}

 

Benefits of automatic boxing and unpacking:

It is easy to operate and simplify the code, so that the basic type and reference type can be calculated directly

Disadvantages: for example, Integer in = null; in = in + 1; exceptions will appear here, and corresponding processing methods must be added

 

Precautions for automatic packing and unpacking:

There's a place where there's a lot of holes in Java interviews,

package demo;

public class IntegerDemo {
    public static void main(String[] args) {
        function1();
        function2();
        function3();
    }

    public static void function1() {
        Integer i = new Integer(1);
        Integer j = new Integer(1);
        System.out.println(i==j);//false
        //Here we compare the addresses of two objects. Of course, they are different
        System.out.println(i.equals(j));//true
        //Here is the data of the comparison object, not the address
    }
    
    public static void function2(){
        Integer a = 500;
        Integer b = 500;
        System.out.println(a==b);//false
        System.out.println(a.equals(b));//true
    }
    
    public static void function3(){
        Integer a = 127;
        Integer b = 127;
        System.out.println(a==b);//true
        //Note here that greater than 128 is false
        //When the data is in bytes In scope, JVM No new objects will be created to save memory
        //Here Integer b = 127 <==> Integer b = a
        System.out.println(a.equals(b));//true
    }
}

Posted by himani on Sun, 03 May 2020 10:58:47 -0700