Data type
Emphasis: in arithmetic expressions, byte,short,char all become int, and then all variables are converted to the highest-level type in their calculation.
Basic data types and reference types
Basic data types: Byte 1, short 2, int 4, long 8, char 2 (equivalent to unsigned integer), float 4, double 8. Boolean
Integer default int type long L = 999999999999 compile error, default int is out of int range, long L = 99999999999L
Floating-point number default double type, 8888.8f for float type
Only floating-point data can be represented as scientific notation, 5200 is integer, and 52E2 is floating-point
Java encoding is a 16-bit Unicode character set, with the first 256 '/u0000 ' to '/u00ff ' and ASCII fully coincident with Unicode
' \ n ' newline ' \ ' ' double quotation mark ' \ \ ' backslash ' \ R ' carriage return
Char ch1 = ' A '; char CH2 = ' mad '; char CH3 = ' \ R '; Char CH4 = ' \u006f ';
Three special floating-point types:
Positive Infinity: Double d = double.positive_infinity flloat f = float.positive_infinity any positive infinity is mostly equal
Negative Infinity: Double d = double.nega_infinity flloat f = float.nega_infinity Any negative infinity is mostly equal
Non-number: Float.nan Double.NaN all NaN are not equal
The floating-point number is divided by 0 Infinity Integer divided by 0 to produce an exception arithmeticexception
Type conversions
If the range of small can be automatically converted to a large range of expression, to represent a large range to be converted to a small range, the conversion must be strengthened. byte,short| | Char-->int-->long-->float-->double
Byte,short,char do not convert to each other, the three before the operation of the uniform conversion to int type, and then calculate, There are many data types mixed calculation, all the data into the largest capacity of the data type, and then the calculation
1 intI1 = 123; 2 intI2 = 456;3 DoubleD1 = (i1+i2) *1.2;//the system will be converted to a double type operation4 floatF1 = (float) ((I1+I2) *1.2);//need to strengthen the conversion character5 byteB1 = 67; 6 byteb2 = 89;7 byteB3 = (byte) (B1+B2);//The system will be converted to an int type operation, which requires8 //Stronger conversion9 //overflow, the system automatically cuts out the bytesTen System.out.println (b3); One DoubleD2 =1e200; A floatF2 = (float) D2;//overflow will be generated, displaying infinity markers - System.out.println (F2); - the floatF3 = 1.23f;//must add f - LongL1 = 123; - LongL2 = 30000000000L;//must be plus l - floatf = l1+l2+f3;//system will be converted to float type calculation + LongL = (Long) F;//The cast will shed the fractional part (not rounding)
View Code
Tip: When any basic data type is connected to a string, it is automatically converted to a string. You want to convert the base data type to a string that can be spliced with an empty string.
1 Boolean true ; 2 double d = 8.2732; 3 System.out.println ("haha" +d+a); 4 // Output Haha8.2732true
View Code
System.out.println (' A ' +1+ "string"); // 98string System.out.println (1+ "string" + ' a '); // 1stringa
Java Basic data types