The article directories
- Basic data type conversion
-
- 1. Automatic type conversion
- 2. Automatic type conversion notes and details
- 3. Cast type
- 4. Cast details
- 5. Basic data type conversion – Exercises
Basic data type conversion
1. Automatic type conversion
// demonstrate automatic conversion
int num = 'a';//ok char -> int
double d1 = 80; //ok int -> double
System.out.println(num);/ / 97
System.out.println(d1);/ / 80.0
Copy the code
2. Automatic type conversion notes and details
// Detail 1: When multiple types of data are mixed,
// The system first automatically converts all data to the data type with the largest capacity, and then performs the calculation
int n1 = 10; //ok
//float d1 = n1 + 1.1; // error n1 + 1.1 => Result type double
//double d1 = n1 + 1.1; N1 + 1.1 => The result type is double
float d1 = n1 + 1.1 F;N1 + 1.1 => The result type is float
// Detail 2: When we assign a high precision data type to a low precision data type,
// An error is reported, otherwise an automatic type conversion is performed.
//
/ / int n2 = 1.1; // double -> int
// Detail 3: (byte, short) and char do not automatically convert to each other
// When assigning a specific number to byte, (1) first checks whether the number is in byte range, if so
byte b1 = 10; / / to - 128-127
// int n2 = 1; / / n2 is int
// byte b2 = n2; // Error, cause: If a variable is assigned, determine the type
//
// char c1 = b1; // Error because byte cannot be automatically converted to char
//
//
// Detail 4: byte, short, char these three can be computed, and are first converted to int when computed
byte b2 = 1;
byte b3 = 2;
short s1 = 1;
//short s2 = b2 + s1; B2 + s1 => int
int s2 = b2 + s1;B2 + s1 => int
//byte b4 = b2 + b3; // Error: b2 + b3 => int
//
// Boolean does not participate in the conversion
boolean pass = true;
//int num100 = pass; // Boolean does not participate in automatic conversion of types
// Automatic promotion principle: The type of the result of an expression is automatically promoted to the largest type of the operand
// Look at a problem
byte b4 = 1;
short s3 = 100;
int num200 = 1;
float num300 = 1.1 F;
double num500 = b4 + s3 + num200 + num300; //float -> double
Copy the code
3. Cast type
- This section describes the reverse process of automatic type conversion to convert large-capacity data types to small-capacity data types. Use the cast character () with special care, but it may cause loss of accuracy or overflow.
- Case presentation
ForceConvert.java
// demonstrate casting
int n1 = (int)1.9;
System.out.println("n1=" + n1);//1, resulting in accuracy loss
int n2 = 2000;
byte b1 = (byte)n2;
System.out.println("b1=" + b1);//-48, causing a data overflow
Copy the code
4. Cast details
- 10 near the parentheses is converted to an int, multiplied by 3.5 to a double, resulting in a double and not an int
// demonstrate casting
// Strong conversions are valid only for the most recent operand. Parentheses are often used to raise the priority
/ / int x = (int) 10 * 3.5 + 6 * 1.5; Error: double -> int
int x = (int) (10*3.5+6*1.5);/ / 44.0 > 44 (int)
System.out.println(x);/ / 44
char c1 = 100; //ok
int m = 100; //ok
//char c2 = m; / / error
char c3 = (char)m; //ok
System.out.println(c3);//100 corresponds to the d character
Copy the code