1. Basic data types in Java include: Integer (Byte, short, int, long), char (char), Boolean (Boolean), floating-point (float, double) three types.
2, the occupied memory is: byte:1byte;short:2byte;int:4byte;long:8byte;char:2byte;boolean:1byte;float:4byte;double:8byte;
3, the range of the number of byte:[-128~127];short:[-32768~32767];int:[-2147483648~2147483647];long:[-9223372036854774808~ 9223372036854774807];
float:[3.402823e+38 ~ 1.401298e-45];double:[1.797693e+308~ 4.9000000e-324];
Represents the calculation of a range:
For example byte type:
00000_0000~0111_1111: Indicates 0~127;
10000_0000~1111_1111: Indicates -128~-1;
10000_0000 is-0; 0 =-128;
4, in Java all the integer data by default is an int type, float type default is double, other types of byte, short will be implicitly converted (if not beyond the expression range); eg.:
1 byte b = ten; // Implicit conversions 2 Short s = ten; // Implicit conversions 3 int i = ten; 4 long l = 10L; // need L or L to indicate 5 float f = 10F; // requires F or F to indicate 6 double d = 10;
5, extended conversion, extended conversion is not necessary to display the type conversion, because the target type is sufficient to accommodate the current type does not lose information.
eg.:
byteb = 10;//Implicit conversions Shorts = 10;//Implicit conversionsinti = 10;LongL = 10L;//need L or L to indicatefloatf = 10F;//requires F or F to indicateDoubleD = 10;//Extended Conversions ShortBUp =b;intSUp =s;LongIUp =i;floatLUp =l;DoubleDUp =F; Charch = ' 1 ';intChup = ch-48;
6, forced type conversion, will accommodate more information to the data type into a smaller capacity of the data type, there may be a risk of loss of precision, the compiler requires the programmer to force type conversion;
eg.
int age = ten; Short Agedown = [short] age; // forcing type conversions does not lose information System.out.println (agedown); int width =; byte widthdown = (byte) width; // forcing type conversions will lose information System.out.println (Widthdown);
Output:
10
-126
float average = 10.5f; int averagedown = (int) average; System.out.println (Averagedown);
Output:
10
Because with = 130 exceeds the representation range of the byte data type, the compiler does a high truncation:
130 Original code: 0000_0000_0000_0000_0000_0000_1000_0010;
Complement: 0000_0000_0000_0000_0000_0000_1000_0010;
130 of the complement intercept Eight is: 1000_0010;
1000_0010; the highest bit for the complement form is the sign bit, the visible result is a negative number, minus a negation to obtain the original code: 1111_1110;
So the output is-126.
The floating-point number truncates the fractional part directly.
The maximum data type result in the usual expression determines the data type of the expression.
7. Exercises:
One or two will compile?
/** * One: */ Short S1 = 1= s1 + 1;
/** * Two: */ Short S1 = 1+ = 1;
Answer: Two
Because all integer data in Java defaults to int type S1 = S1 + 1, 1 for int requires mandatory reload, and S1 + = 1; The compiler makes implicit type conversions.
Write the first article written well square/(ㄒoㄒ)/~~ .....
Java basic data types and data type conversions