We can understand variables in the Java language as containers for storing data. Only if they are defined in advance can we assign values to the container and then participate in operations, store the desired results, etc., so we must first learn how to define these variable.
In this article, we will focus on the basic data types in the Java language, that is, built-in types. The Java language provides a total of eight basic types, including six numeric types (four integer types, two floating point types), and one character type. , there is also a Boolean type, which is introduced in turn:
1. byte type:
The byte data type is an 8-bit, signed, two's complement integer representation.
The minimum value is -128 (-2^7), the maximum value is 127 (2^7-1), and the default value is 0.
The byte type is used to save space in large arrays, mainly replacing integers, because the byte variable occupies only one-quarter of the space of the int type.
example:
bytea=150,byteb=-30;
2. short type:
The short data type is a 16-bit, signed two's complement integer.
The minimum value is -32768 (-2^15), and the maximum value is 32767 (2^15 - 1).
The short data type can also save space like byte. A short variable is half the space occupied by an int type variable, and the default value is 0.
example:
shorts=200, shortr=-4500;
3. int type:
The int data type is a 32-bit, signed two's complement representation of an integer.
The minimum value is -2,147,483,648 (-2^31), and the maximum value is 2,147,483,647 (2^31 - 1). Generally, integer variables default to int type, and the default value is 0.
example:
inta=100000,intb=-200000;
4. long type:
The long data type is a 64-bit, signed two's complement integer representation.
The minimum value is -9,223,372,036,854,775,808 (-2^63), and the maximum value is 9,223,372,036,854,775,807 (2^63 - 1). This type is mainly used on systems that need to compare large integers. The default value is 0L.
example:
longa=100000L,Longb=-200000L;
L is theoretically not case-sensitive, but if it is written as l, it will easily be confused with the number 1 and difficult to distinguish, so it is best to capitalize it.
5. float type:
The float data type is a single-precision, 32-bit, IEEE 754-compliant floating point number.
float can save memory space when storing large floating point arrays. The default value is 0.0f.
Floating point numbers cannot be used to represent precise values such as currencies.
example:
floatf1=3.1415f;
6. double type:
The double data type is a double-precision, 64-bit, IEEE 754-compliant floating point number.
The default type of floating point numbers is double.
The double type also cannot represent precise values, such as currency, and the default value is 0.0d.
7. boolean type:
The boolean data type represents one bit of information.
There are only two values: true and false.
This type only serves as a flag to record true/false conditions.
The default value is false.
example:
booleanResult=true;
8. char type:
The char type is a single 16-bit Unicode character.
The minimum value is u0000 (the decimal equivalent is 0), and the maximum value is uffff (which is 65535).
The char data type can store any character.
example:
charletter='A';
The default values of various data types are listed below:
You can understand the above basic data types in Java without memorizing them by rote. In subsequent code exercises, you can know which data type to use according to actual needs. As the amount of code increases, various data types will be understood. But in the chest.