Zero-basic learning java: variable (2), Basic Learning java variable
The concepts and data types of variables are introduced above. Now we will introduce the conversion between several data types of variables.
1. Automatic type conversion: the data type with a small capacity is automatically converted to a data type with a large capacity. Data types are sorted by capacity:
When there are multiple types of data hybrid operations, the system first automatically converts all the data to the data type with the largest capacity, and then computes.
Byte, short, and char are not converted to each other. They are first converted to the int type during calculation.
When you concatenate any basic type value and string value (+), the value of the basic type is automatically converted to the string type.
2. Forced type conversion
The inverse process of automatic type conversion converts a large data type to a small data type. The forced conversion character () must be added during use, but may reduce the precision or overflow. Pay special attention to this.
Generally, strings cannot be directly converted to the basic type, but strings can be converted to the basic type through the packaging class corresponding to the basic type.
For example, String a = "43"; int I = Integer. parseInt ();
The boolean type cannot be converted to another data type.
Case:
/*
Calculation between variables: (boolean is not considered. Remaining: char byte short int long floatdouble)
1. Automatic type conversion
2. Forced type conversion
*/
ClassTestVeriable1 {
PublicstaticvoidMain (String [] args ){
// 1. Automatic type conversion: when the data type with a small capacity is computed with the data type with a large capacity, the data type with a small capacity is automatically converted
// Large data types: char, byte, short ==> int ==> long ==> float === double
IntI1 = 12;
ShortS1 = 2;
IntI2 = i1 + s1;
FloatF1 = 12.3F;
FloatF2 = f1 + i2;
// Float d1 = f2 + 12.3;
LongL = 12L;
FloatF3 = l;
System. out. println (i2 );
System. out. println (f2 );
CharC1 = 'a'; // 97
C1 = 'a'; // 65
IntI3 = c1 + 1;
System. out. println (i3 );
// Note: When calculation is performed between char, byte, and short, the default result is int type.
ShortSs1 = 12;
ByteBb1 = 1;
CharC0 = 'a ';
// Short ss2 = ss1 + bb1;
IntIi1 = ss1 + bb1;
// Char cc2 = maid + bb1;
IntIi2 = maid + bb1;
ShortSs2 = 11;
// Short ss3 = ss1 + ss2;
// 2. Forced type conversion: Large Capacity to small capacity. Use the forced type conversion character :()
// Forced type conversion problem: loss of Precision
LongL1 = 12345L;
IntM1 = (Int) L1;
System. out. println (m1 );
ByteBy1 = (Byte) M1;
System. out. println (by1 );
// Commonly used String, also a data type: String
String nation = "I am a Chinese ";
System. out. println (nation );
// Operation between the string and the basic data type: only join operation: +. The result is still a string.
String str = "abc ";
String str1 = str + m1; // abc12345
System. out. println (str1 );
// Question:
String st1 = "hello ";
IntMyInt1 = 12;
CharOutputs = 'a'; // 97
System. out. println (str1 + myInt1 + timeout); // hello12a
System. out. println (myInt1 + bytes + str1); // 109 hello
System. out. println (Bytes + str1 + myInt1); // ahello12
String st2 = "12 ";
Str2 = 12 + "";
}
}
Exercise:
String str1 = 4; // right or wrong: Error
String str2 = 3.5f + ""; // judge str2 right or wrong: For System. out. println (str2); // output: 3.5
System. out. println (3 + 4 + "Hello !"); // Output: 7 Hello!
System. out. println ("Hello !" + 3 + 4); // output: Hello! 34
System. out. println ('A' + 1 + "Hello !"); // Output: 98 Hello!
System. out. println ("Hello" + 'A' + 1); // output: Hello! A1
If you are interested in java and want to learn more about java, please add (858568103). Here we have a java expert to help you solve the problem.