JavaSE-Common Classes (13)

1. Basic concepts
  • Concept: Basic data types correspond to reference types, which are called packaging classes of basic data types.
2. Packaging classes corresponding to basic data types:
Basic data type Package name
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

Three, data conversion [understand]

  1. Conversion between int and Integer

    (1) int -----> Integer

    a. Integer i1 = new Integer(12); // With construction method

    b. Integer i2 = Integer.valueOf(12); // With the help of static methods in the Integer class

    (2) Integer ----> int

    a. int a1 = i1.intValue(); // With the help of the member method intValue() in the Integer class

  2. Conversion between String and Integer

    (1) String ----> Integer

    a. Integer i3 = new Integer("123"); // With the help of the construction method in the Integer class

    b. Integer i4 = Integer.valueOf("123"); // With the help of static methods in the Integer class

    (2)Integer ----> String

    a. String s1 = i4.toString();

    b. String s2 = i4+""; //With the help of string splicing

  3. Conversion between int and String

    (1) int —> String

    a. int a = 3; String s3 = a+"";

    (2) String ----> int 【Key points of development and application】

    int a = Integer.parseInt("123"); // With the help of static methods in the Integer class

    Note: To convert String type data to int/Integer type data, the String data type to be converted must be a pure number, otherwise the operation will report an error, the error message is as follows:

    java.lang.NumberFormatException (number format conversion exception).

Four, automatic packing, automatic unpacking
  1. After JDK5.0, the functions of automatic boxing and automatic unboxing are provided, and the basic data types and corresponding packaging types can be automatically converted to each other.

  2. Auto-packing: Basic data types can be directly assigned to their corresponding packaging types.

    Automatic unboxing: The reference of the packaging type can be directly assigned to the variable of the corresponding basic data type.

  3. Cases of automatic packing and unpacking:

    Integer i = 12;  // 自动装箱
    int a = i ; // 自动拆箱
  4. The principle of automatic boxing: the static valueOf method in the Integer class when the bottom layer of automatic boxing is called:

    public static Integer valueOf(int i) {
          
          
    	if (i >= -128 && i <= 127)
    		return IntegerCache.cache[i+(-IntegerCache.low)];
    	// 如果 传入数据不在-128~127之间,则new Integer类型对象	
        return new Integer(i); 
    }

    Note: Java has pre-packaged the content of the -128 127 common data segment. As long as the converted data is between -128 and 127, you don't need to create an object every time, but can be directly obtained from the processing result, -128~ 127 The processing result is stored in the buffer and constant pool.

    Interview question: Analyze the results of the printout of two print statements and explain the reasons.

    ​ Integer i1 = 12;
    Integer i2 = 12;

    ​ System.out.println(i1==i2); // true

    ​ Integer i3 = 150;

    ​ Integer i4 = 150 ;

    ​ System.out.println(i3==i4); // false

5. Actual development and application of packaging [Development focus]
  1. It can be used to distinguish between valid data and invalid data: for example, 0 and null, 0.0 and null.

  2. Scenario: During development, the attributes in the class are usually defined as the package type corresponding to the basic data type. Distinguish between valid data and invalid data, such as null and 0, null and 0.0, etc.

  3. The case is as follows:

    public class TestStudent {
          
          
    	public static void main(String[] args) {
          
          
    		Student s1 = new Student("张三",23);
    		Student s2 = new Student("lily",25,0.0);
    		
    		System.out.println(s1);
    		System.out.println(s2);
            
            /* 打印输出结果如下:
               name=张三, age=23, score=null(没有考试)
    		   name=lily, age=25, score=0.0(成绩为0分)
            */
    	}
    }
    class Student {
          
          
    	private String name;
    	private Integer age ;
    	private Double score;
    	public Student() {
          
          }
    	public Student(String name, Integer age) {
          
          
    		super();
    		this.name = name;
    		this.age = age;
    	}
    	public Student(String name, Integer age, Double score) {
          
          
    		this.name = name;
    		this.age = age;
    		this.score = score;
    	}
    	public String getName() {
          
          
    		return name;
    	}
    	public void setName(String name) {
          
          
    		this.name = name;
    	}
    	public Integer getAge() {
          
          
    		return age;
    	}
    	public void setAge(Integer age) {
          
          
    		this.age = age;
    	}
    	public Double getScore() {
          
          
    		return score;
    	}
    	public void setScore(Double score) {
          
          
    		this.score = score;
    	}
    	@Override
    	public String toString() {
          
          
    		return "name=" + name + ", age=" + age + ", 
    		          score=" + score;
    	}
    }

Guess you like

Origin blog.csdn.net/Java_lover_zpark/article/details/105320135