Java: A brief description of basic data type packaging classes

Basic data type packaging class

Before sharing today’s content, we need to know: In actual program use, the data entered by the user on the program interface is stored in character type, but we know that there are 8 basic data types in Java, and we use The frequency of non-character data types is also quite high. That is to say, for program development, we need to convert string data into specified basic data types according to requirements. For example, age needs to be converted to int type, and grades need to be converted. This is obviously very troublesome. However, in Java, we provide us with corresponding objects to solve this problem: basic data type object packaging class, that is, Java encapsulates basic data types into objects. The packaging categories corresponding to the eight basic types are as follows:

Types of English Corresponding packaging
Byte type byte Byte
Short integer short Short
Integer int Integer
Long integer long Long
Character type char Character
Boolean boolean Boolean
Floating point float Float
Floating point double Double

Taking into account that most of the usage of the basic data type packaging classes are consistent, here we choose Integer as an example to share the usage of the basic data type packaging classes.

Convert a string to a basic type (parseXXX(String s))

parseXXX(String s); where XXX represents a basic data type, and the parameter is a string that can be converted into a basic data type. Note: If the string cannot be converted into a basic data type, the problem of number conversion will occur. NumberFormatException
Example 1:

package demo;

public class IntegerDemo {
    
    
	public static void main(String[] args) {
    
    
		function();
	}
	/*
	 * Interger类中的静态方法parseInt(String s),返回基本数据类型
	 * 要求:字符串必须是数字格式的
	 */
	public static void function() {
    
    
		int i = Integer.parseInt("16");
		System.out.println(i/2);
	}
}

In this example, the string "16" is converted to the number 16, and then the operation is performed to output 8. It should be noted that the "16" here cannot be entered as a non-numeric character string such as "16", otherwise an error will be reported.
Example 2:

package demo;

public class IntegerDemo {
    
    
	public static void main(String[] args) {
    
    
		function();
	}
	/*
	 * Interger类中的静态方法parseInt(String s,int radix)
	 * 
	 */
	public static void function() {
    
    
		int i = Integer.parseInt("110",2);
		System.out.println(i);
	}
}

The method in the above example is an overloaded method of parseInt(String s), which is used to convert binary to decimal. The radix parameter in the method represents the base (base), here this parameter can be changed to octal or hexadecimal These are in bases, but after changing radix, the previous String s parameters must conform to the corresponding base rules, for example, radix = 2, String s = "112", the String s parameters here do not conform to binary.

Three methods to convert basic data types to strings

(1) Basic data type + "";
example:

package demo;

public class IntegerDemo {
    
    
	public static void main(String[] args) {
    
    
		function();
	}
	public static void function() {
    
    
		int i = 10;
		String s = i +"";
		System.out.println(i+1);
	}
}

(2) The static method toString() in the Interger class;
example:

package demo;

public class IntegerDemo {
    
    
	public static void main(String[] args) {
    
    
		function();
	}
	public static void function() {
    
    
		int i = 10;
		String s = Integer.toString(i);
		System.out.println(s);
	}
}

(3) The static method toString(int a, int radix) in the Interger class;
example:

package demo;

public class IntegerDemo {
    
    
	public static void main(String[] args) {
    
    
		function();
	}
	public static void function() {
    
    
		int i = 5;
		String s = Integer.toString(i,2);
		System.out.println(s);
	}
}

The method in Example 2 means to convert i into a binary string, which is different from the meaning of the above parseInt(String s, index) method

Integer construction method

example:

package demo;

public class IntegerDemo {
    
    
	public static void main(String[] args) {
    
    
		function();
	}
	public static void function() {
    
    
	Integer in = new Integer("100");
	int i = in.intValue();
	System.out.println(i);
			
	}
}

The meaning of this example is to call the constructor of the Integer class, pass the character in the numeric format to the constructor of the Integer class, create an object of the Integer class, wrap the string, and convert the string to a basic data type You also need to call the inValue() method;

Other methods in the Integer class

(1) Two static member variables
MAX_VALUE and
MIN_VALUE
These two variables represent the maximum and minimum values ​​of the type. If you need to know the value range of the rest of the basic types, you only need to call it with the corresponding wrapper class, such as the long type. Yes: Long.MAX_VALUE Long.MIN_VALUE
(2) static method toBinarString(int)
decimal to binary
(3) static method toOctalString(int)
decimal to octal
(4) static method toHexString(int)
decimal to hexadecimal

Auto-boxing and auto-unboxing

Automatic boxing: the basic data type directly becomes the object.
Automatic unboxing: the data in the object returns to the basic data type

	Integer in = 1;//自动装箱 相当于Integer in = new Integer(1);

		in = in +1;//等号右边:将in对象转换成基本数值(自动拆箱)in.intValue() + 1;加法运算后,再次装箱,把基本数值转换成对象

Advantages of automatic boxing and unboxing: saving code, basic types and reference types can be directly calculated, convenient operation
Disadvantages of automatic boxing and unboxing: easy to appear Integer in = null; code, it will report a null pointer exception

Guess you like

Origin blog.csdn.net/qq_43825377/article/details/109108976