9--Conversion between basic data types

Java is a mandatory type language. The data type is already determined when the variable is defined, so it cannot be converted to other data types at will, but Java allows limited type conversion processing. Data type conversion in Java is divided into "automatic type conversion" and "coercive type conversion".

Automatic type conversion

The variable of the data type has been defined in the program. If you want to use another data type to represent, Java will automatically
convert the data type if the following conditions are met: 1. The range of the data type before the conversion is more than the conversion. The scope of the latter data type is small
2. The data type before conversion is compatible with the data type after conversion (except for the other 7 basic data types of boolean type)
Examples:

public class Demo9_1{
    
    
	public static void main(String[] args){
    
    
		byte num1 = 12;
		//byte → int
		int num2 = num1;
	}
}

Schematic diagram of automatic type conversion
Insert picture description here

Note: The size of the capacity at this time refers to the large and small range of the number. For example: the float capacity is greater than the long capacity.
Special attention: when the three types of variables of byte, char, and short are operated, the result is int type.
Examples of errors:

public class Demo9_2{
    
    
	public static void main(String[] args){
    
    
		byte num1 = 12;
		byte num2 = 20;
		//编译失败: 错误: 不兼容的类型: 从int转换到byte可能会有损失
		byte rs= num1 + num2;
		System.out.println(rs);
	}
}

Examples of corrections:

public class Demo9_2{
    
    
	public static void main(String[] args){
    
    
		byte num1 = 12;
		byte num2 = 20;
		int rs= num1 + num2;
		System.out.println(rs);
	}
}

Examples:

public class Demo9_3{
    
    
	public static void main(String[] args){
    
    
		int num1 = 12;
		//int → float
		float num2 = num1;
		System.out.println(num2);
	}
}

to sum up

  1. When there are multiple types of data mixed operations, the system first automatically converts all data into the data type with the largest capacity, and then performs calculations.
  2. There is no mutual conversion between byte, short, and char. The three of them are first converted to int type during calculation.
  3. The boolean type cannot be operated on with other data types
  4. When the value of any basic data type and the string (String) are connected (+), the value of the basic data type will automatically be converted to the string (String) type.

Forced type conversion

The inverse process of automatic type conversion converts a large range of data types into a small range of data types. Accuracy may be lost during the conversion process. Although it is a forced type conversion, it must also meet type compatibility (except for the boolean type, the other 7 data types).
note:

  1. When using, add a coercion symbol: ()
  2. It may cause a decrease in accuracy or an overflow, so be careful.
    Examples:
public class Demo9_4{
    
    
	public static void main(String[] args){
    
    
		double num1 = 12.9;
		//精度损失
		int num2 = (int)num1;//截断操作
		System.out.println(num2);
		
		//没有精度损失
		long num3 = 123;
		short num4 = (short)num3;
		
		//精度损失
		int num5 = 128;
		byte num6 = (byte)num5;
		System.out.println(num6);//-128
	}
}

to sum up

  1. The inverse process of automatic type conversion converts a data type with a large capacity to a data type with a small capacity. When using it, you must add the coercion symbol: (), but it may cause a decrease in accuracy or overflow, so pay special attention
  2. Generally, a string cannot be directly converted to a basic type, but it can be converted to a basic type through a wrapper class corresponding to the basic type.
  3. The boolean type cannot be converted to other data types

Supplement: String type
1. String is not a basic data type, but a reference data type
. 2. The usage method is consistent with the basic data type. For example: String str = "abcd";
3. All data types (basic data types and reference data types) encounter a string, they will automatically type conversion to the string
4. About the use of the string later.
Example:

public class Demo9_5{
    
    
	public static void main(String[] args){
    
    
		int num1 = 3;
		int num3 = 7;
		// 以下两个 +  表示连接符
		String str= "abc" + num1 + num3;
		System.out.println(str);//abc37
		//以下第一个 + 表示连接符,第二个 + 表示 加号
		String str2="abc" + (num1 + num3);
		System.out.println(str2);//abc10
	}
}

Guess you like

Origin blog.csdn.net/qwy715229258163/article/details/113705000