Java data type, java
Java is a strongly typed programming language, indicating that all variables used in java programs must be clearly defined data types.
Java data types can be divided into basic data types and reference data types.
Basic Data Type:
Numeric type: Integer type (byte, short, int, long), floating point type (float, double)
Character Type: char
Boolean Type: boolean
Reference data type:
Class, array, and interface)
The difference between the basic data type and the reference data type is that the variables and object handles of the basic data type are stored in the stack memory, which occupies a fixed space and can be directly accessed through the variable name; all java objects are stored in the heap memory. The heap memory is a runtime data zone that occupies any size of space and needs to be directly accessed through the object reference stored in the stack memory.
The following describes the basic data types.
Java defines four types and eight basic data types.
Integer type: byte, short, int, long
Floating point type: float and double
Character Type: char
Boolean Type: boolean
Basic Data Types in java
| Data Type |
Memory space occupied (bytes) |
Value Range |
Default Value |
| Byte |
1 |
-128 ~ 127 |
0 |
| Short |
2 |
-32768 ~ 32767 |
0 |
| Int |
4 |
-2147483648 ~ 21474881647 |
0 |
| Long |
8 |
-9223372036854775808 ~ 9223372036854775807 |
0L |
| Float |
4 |
1.4E-45 ~ 3.4028235E38 |
0.0f |
| Double |
8 |
4.9E-324 ~ 1.7976931348623157E308 |
0.0d |
| Char |
2 |
'\ U000000 '~ '\ Uffff' |
'\ U000000' |
| Boolean |
1 |
True, flase |
Flase |
In java, the basic data type occupies a fixed Memory Length, regardless of the specific software and hardware platform environment.
Constant
A constant is the amount of data that cannot be changed during program execution. To declare a constant, use the keyword final.
Note:
(1) The naming rule for constants is that each letter is capitalized.
(2) If you want to assign a value to a constant during execution, compilation will fail.
In java, constants are generally classified into Integer constants, floating-point constants, Boolean constants, character constants, and string constants.
1. Integer constants
In java, Integer constants can be divided into the following three types:
(1) decimal INTEGER: the constant of a decimal integer is from 0 ~ A numerical sequence consisting of 9, and the first digit of the sequence cannot be 0 (except for a single 0 ).
(2) octal INTEGER: the first digit of an octal constant is 0, followed by 0 ~ A numerical sequence consisting of 7.
(3) hexadecimal INTEGER: A hexadecimal integer constant starts with "0x" or "0X", followed by a hexadecimal number sequence. The hexadecimal sequence of numbers is composed of numbers 0 ~ 9 and letter ~ F.
2. Floating Point Constants
A floating-point constant is a numerical constant that can contain decimals. Depending on the length of memory occupied, floating-point constants can be divided into single-precision floating-point constants and double-precision floating-point constants. A single-Precision Constant occupies 4 character segments. After a number is followed by an f or F, a double-precision constant occupies 8 bytes, And a number is followed by d or D. D Or D after a double-precision constant can be omitted. If a floating-point constant does not contain letters, the double-precision floating-point constant is used by default.
Floating-point constants can only be expressed in decimal notation, with traditional counting methods and scientific notation.
(1) Traditional counting: it consists of integer, decimal point, and decimal point.
(2) Scientific Notation: When a number is large or small, scientific notation is used. Example: 1.23e3
3. boolean Constants
Boolean constants have only two values: true and false.
4. character constants
A character constant is a single character enclosed by a pair of single quotes. For example, in 'Q', the character data in java is a 16-bit unsigned data type and uses the Unicode Character Set.
5. string constants
A String constant is a sequence of 0 or more characters enclosed by double quotes. A String constant can contain escape characters.
Variable
Variable refers to the amount of variable values that can be changed during program execution. Variables play a very important role in the program. It can be divided into integer variables, floating point variables, complex variables, and Boolean variables.
Conversion Between Basic Data Types
Data type conversion refers to the conversion of constants or variables from one data type to another. In java, the conversion of basic data types mainly includes automatic conversion and forced type conversion.
1. automatic conversion
Automatic conversion refers to the automatic conversion of data types from low-precision data to high-precision data. Link;
Byte --> short --> int --> long --> float --> double
Char --> int
Note: When the data size is large, int and long types may be converted to float type and long type to double type, which may cause loss of precision.
2. Forced type conversion
Forced type conversion refers to the forced conversion of data types from high precision to low precision. The forced type conversion character "(type)" is required )". Syntax:
(Target type) expression;
Example: float f = 88.88f;
Int x = (int) f; // forcibly converts the float type to the int type.