Basic data type, package type, reference type

Basic types of Java and their corresponding wrapper classes

Basic typeBinary digitPackaging type
boolean1Boolean
byte8Byte
char16Character
short16Short
int32Integer
long64Long
float32Float
double64Double

Automatic packing and unpacking

When a built-in data type is used as an object, the compiler will box the built-in type into a wrapper class. Similarly, the compiler can unpack an object into a built-in type. The Number class belongs to Java Lang package.

public class Demo {
    public static void main(String[] args) {
        //The basic type is assigned to the wrapper class and boxed, which is equivalent to Integer i = new Integer(4)
        Integer i = 4;

        //Unpacking occurs when the package class is operated
        i += 10;
        System.out.println(i);
    }
}

Java wrapper class constant pool

The eight basic types of Java (Byte, Short, Integer, Long, Character, Boolean, Float, Double), except Float and Double, the other six implement constant pools, but they only use constant pools when they are greater than or equal to - 128 and less than or equal to 127.

public static void main(String[] args) {  
        //Constant pool test
        System.out.println("Integer Test of");
        Integer a = 127;  
        Integer b = 127;   
        System.out.println(a == b);  

        a = 128;  
        b = 128;   
        System.out.println(a == b);   

        a = -128;  
        b = -128;   
        System.out.println(a == b);  

        a = -129;  
        b = -129;   
        System.out.println(a == b);  

        // Test Boolean  
        System.out.println("test Boolean");  
        Boolean c = true;  
        Boolean d = true;  
        System.out.println(c == d);  
        d = new Boolean(true);  
        System.out.println(c == d);  
}  

/*
Operation results:
Integer Test of
true
false
true
false
 Test Boolean
true
false
 */

Common methods of packaging

xxxValue method: convert the Number object to the value of xxx data type and return (packing type → basic type).

public class Demo {
    public static void main(String[] args) {
        Integer i = 5;

        //Return byte basic data type
        System.out.println(i.byteValue()); //5
        //Return double basic data type
        System.out.println(i.doubleValue()); //5.0
        //Return long basic data type
        System.out.println(i.longValue()); //5
    }
}

compareTo() method: compare the number object with the parameter, and compare two data of the same type.

  • Returns 0 if the specified number is equal to the parameter
  • If the specified number is less than the parameter, - 1 is returned
  • Returns 1 if the specified number is greater than the parameter
public class Demo {
    public static void main(String[] args) {
        Integer i = 5;

        System.out.println(i.compareTo(3)); //1
        System.out.println(i.compareTo(5)); //0
        System.out.println(i.compareTo(8)); //-1
    }
}

equals() method: judge whether the number object is equal to the parameter.

public class Demo {
    public static void main(String[] args) {
        Integer a = 5;
        Integer b = 10;
        Integer c = 5;
        Short d = 5;

        System.out.println(a.equals(b)); //false
        System.out.println(a.equals(c)); //true
        System.out.println(a.equals(d)); //false
    }
}

valueOf() method: returns the native Number object value of the given parameter. This method is a static method (basic type → wrapper type).

public class Demo {
    public static void main(String[] args) {
        Integer a =Integer.valueOf(9);
        Double b = Double.valueOf(5);
        Float c = Float.valueOf("80");
        Integer d = Integer.valueOf("32", 8);

        System.out.println(a); //9
        System.out.println(b); //5.0
        System.out.println(c); //80.0
        System.out.println(d); //26 (octal)
    }
}

toString() method: returns a value as a string.

public class Demo {
    public static void main(String[] args) {
        Integer a = Integer.valueOf(9);

        System.out.println(a.toString()); //9
        System.out.println(Integer.toString(12)); //12
    }
}

parselnt(): parses a string into a basic data type (string → basic type).

public class Demo {
    public static void main(String[] args) {
        int a =Integer.parseInt("9");
        double b = Double.parseDouble("5");
        int c = Integer.parseInt("32", 8);

        System.out.println(a); //9
        System.out.println(b); //5.0
        System.out.println(c); //26
    }
}

To xxxstring (xxx): convert XXX to XXX base.

public class Demo {
    public static void main(String[] args) {
        String c = Integer.toBinaryString(26);

        System.out.println(c); //Binary 11010
    }
}

Java reference type

Java has five reference types (object types): class interface array enumeration annotation

Reference type: the underlying structure is quite different from the basic type
JVM memory space:
(1) . Heap heap space: allocate object new Student()
(2) . Stack stack space: temporary variable Student stu
(3) . Code code area: class definition, static resource Student.class

Student stu = new Student(); //new creates an object in the heap space of memory
stu.study();                   //Assign the address of the object to the stu reference variable

Implementation steps of the above example:
a. The JVM loads student.xml Class to Code area
b.new Student() allocates space in heap space and creates a Student instance
c. Assign the address of this instance to the reference stu and the stack space

Added by csaba on Fri, 24 Dec 2021 02:00:19 +0200