There are eight basic data types in Java, each of which is
byte, short, int, long, float, double, char, Boolean
1. Integral type
where byte, short, int, long all represent integers, except that their range of values is not the same
byte has a value range of -128~127 and occupies 1 bytes (-2 of 7 to 2 of 7) (1)
Short has a value range of -32768~32767, which takes 2 bytes (-2 of 15 to 2 15-1)
the value range of int is ( -2147483648~2147483647), which takes 4 bytes (-2 of 31 to 2 31-1)
The Long value range is ( -9223372036854774808~9223372036854774807), which takes up 8 bytes (-2 of the 63-to-2 63-square-1)
you can see that the value range of byte and short is small, and the range of long is too large, occupy more space, basically int can meet our daily calculations, and int is also the most used integer type.
in general, if there is an integer number such as 35 in Java, then this number is the int type, if we want it to be a byte type, we can add the uppercase b:35b after the data, indicating that it is byte type, the same 35S means short type, 35L means long, which means int we can do nothing, but if you want to represent a long type, be sure to add "L" after the data.
2, floating-point type
float and double are data types that represent floating-point type, the difference between them is that their accuracy is different
float 3.402823e+38 ~ 1.401298e-45 (e+38 is multiplied by 10 for 38 times, similarly, e-45 means 10 minus 45 times) consuming 4 bytes
Double 1.797693e+308~ 4.9000000e-324 takes 8 bytes
double type more than float type storage range, more accurate, so the usual floating-point data is not declared in the case of double type, if you want to indicate that a data is float type, you can add "F" after the data.
The floating-point data is not completely accurate, so sometimes it is normal to have floating in the last few decimal places when calculating.
3, Boolean type (Boolean)
This type has only two values, True and False (true and non-true)
Boolean t = true; Boolean f = false;
4, Char type (text type)
The data type used to hold characters, 2 bytes, Unicode encoded, and ASCII compatible with the first 128 bytes of encoding
character storage range in \u0000~\uffff, when defining the character type of data should pay attention to ", such as ' 1 ' means the character ' 1 ' instead of the value 1,
char c = ' 1 ';
we try to output C and see, System.out.println (c); The result is 1, and if we output it like this System.out.println (c+0);
it turns out to be 49.
If we define C like this, look at
char c = ' \u0031 '; The result of the output is still 1, because the character ' 1 ' corresponds to the Unicode encoding that is \u0031
char c1 = ' h ', c2 = ' e ', c3= ' l ', c4= ' l ', c5 = ' o '; System.out.print (C1); System.out.print (C2); System.out.print (C3); System.out.print (C4); Sytem.out.print (C5);
Java basic data Type value range