Java is a "strongly typed" language, and you must specify the data type when declaring a variable. Variable (variable) occupies a certain amount of memory space. Different types of variables occupy different sizes.
Java has 8 basic data types, including 4 integer, 2 floating-point, 1-character, 1-Boolean, see the table below.
Java basic data Types
| Data Type |
Description |
occupied memory |
Example |
Notes |
| Byte |
BYTE type |
1 byte |
3, 127 |
|
| Short |
Short-integer |
2 bytes |
3, 32767 |
|
| Int |
Integral type |
4 bytes |
3, 21474836 |
|
| Long |
Long integer type |
8 bytes |
3L, 92233720368L |
Long finally has an L letter (case-insensitive). |
| Float |
Single-precision floating-point type |
4 bytes |
1.2F, 223.56F |
Float finally has an F letter (capitalization doesn't matter). |
| Double |
Double-precision floating-point type |
8 bytes |
1.2, 1.2D, 223.56, 223.56D |
Double finally preferably have a D letter (case does not matter). |
| Char |
Character type |
2 bytes |
' A ', ' a ' |
Character data can only be one character, surrounded by single quotes. |
| Boolean |
Boolean type |
1 bit |
True, False |
|
For integer data, the type of int is typically used. But if the energy emitted by the atom bomb that is served on Hiroshima Nagasaki is used, a long type is required. The byte and short types are primarily used for specific applications, such as the underlying file processing or the need to control large arrays that occupy the amount of storage space.
In Java, the length of the integer data is platform-independent, which solves many of the problems that the programmer brings to programmers when porting software from one platform to another. In contrast, the length of a/C + + Integer data is platform-dependent, and programmers need to choose the right integer for different platforms, which could result in an integer overflow on a 32-bit system for programs that run stably on a 64-bit system.
Octal has a prefix of 0, for example 010 corresponds to 8 in decimal, hexadecimal has a prefix of 0x, such as 0xCAFE; starting with Java 7, you can use prefix 0b to represent binary data, such as the 0b1001 corresponding to 9 in decimal. Also starting with Java 7, you can use underscores to separate numbers, like the English numeral notation, such as 1_000_000 1,000,000, or 1 million. The underscore is just to make the code easier to read, and the compiler will remove these underscores.
Also, unlike C/c++,java, unsigned types (unsigned) are not supported.
The valid digits of the float type are up to 7 bits long, and the valid number lengths include integer and fractional parts. For example:
- float x = 223. 56F;
- Float y = 00f;
Note: There is a flag "F" or "F" behind each float type, and this flag represents the float type.
A double type is a valid number with a maximum of 15 bits. As with the float type, a double is followed by a flag "D" or "D". For example:
- Double x = 45D;
- Double y = 422. 22d;
- Double Z = 562.234;
Note: floating-point data without any flags, the system defaults to the double type.
In most cases, a double type is used, and the precision of float is difficult to meet demand.
Examples of application of different data types:
- Public class Demo {
- public static void main(String[] args){
- //character type
- char webName1 = ' micro ';
- char webName2 = ' learning ';
- char WebName3 = ' Garden ';
- System. Out. println("website name is:" + webName1 + webName2 + webName3);
- //integral type
- Short x=$; //Decimal
- int y=022; //Eight binary
- long Z=0x22l; //16 binary
- System. Out. println("converted to decimal: x =" + x + ", y =" + y + ", z =" + z ");
- //floating-point type
- float m = 45f;
- double N = ten;
- System. Out. println("Calculated product:" + M + "*" + N + "=" + M*n);
- }
- }
Operation Result:
The name of the website is: Micro School
Convert to decimal: x = $, y =, z = 34
Calculated product: 22.45 * 10.0=224.50000762939453
As you can see from the running result, even if the floating-point data has only integers with no decimals, the system will automatically add a decimal point to the output of the console, and the scale is all set to 0.
Description of the Boolean type
If you have programming experience and understand the Boolean type, please skip the following tutorial, the following tutorials are for readers with only C language basis (C language does not have a Boolean type).
In the C language, if the condition is determined, it returns 1, otherwise 0 is returned, for example:
- #include <stdio.h>
- int main(){
- int x = >;
- int y = <;
- printf("100>10 =%d\ n", x);
- printf("100<10 =%d\ n", y);
- return 0;
- }
Operation Result:
100>10 = 1
100<10 = 0
In Java, however, the condition returns True, otherwise false, which is the Boolean type. For example:
- Public class Demo {
- public static void main(String[] args){
- //character type
- Boolean a = >;
- Boolean b = <;
- System. Out. println("100>10 =" + a);
- System. Out. println("100<10 =" + b);
- if(a){
- System. Out. println("100<10 is Right");
- }Else{
- System. Out. println("100<10 is Wrong");
- }
- }
- }
Operation Result:
100>10 = True
100<10 = False
100<10 was right.
In fact, true equates to 1,false equivalent to 0, except for a different name, and becomes a data type alone.
2.Java data types and variable definitions