Overview of Java Data Types
Type | Size | Default Value | Value Range | Example |
---|---|---|---|---|
byte | 1 byte | 0 | -128 to 127 | byte b = 100; |
short | 2 bytes | 0 | -32,768 to 32,767 | short s = 10000; |
int | 4 bytes | 0 | -2B to 2B | int i = 123456; |
long | 8 bytes | 0L | ±9 quintillion | long l = 123L; |
float | 4 bytes | 0.0f | ±3.4E38 (approx) | float f = 3.14f; |
double | 8 bytes | 0.0d | ±1.8E308 (approx) | double d = 2.718; |
char | 2 bytes | '\u0000' | 0 to 65,535 | char c = 'A'; |
boolean | 1 bit* | false | true / false | boolean b = true; |
▲ Classification of Data Types
In Java, data types are divided into primitive data types and reference data types, and both are crucial for understanding memory management and data models.
In the Java programming language, data types are carefully divided into two major categories: primitive data types and reference data types. The distinction between these two types is essential for understanding Java’s data model and memory management.
▲ Eight Primitive Data Types
The eight primitive data types in Java include: byte, short, int, and long. These data types differ in length and the range of data they can represent. For programmers, understanding the characteristics of these types is the foundation for writing efficient and accurate code.
64 – 2^64 ~ 2^64 – 1
float (single – precision floating – point type)
32 – 3.4E38 (-3.4 × 10^38) ~ 3.4E38 (3.4 × 10^38)
double (double – precision floating – point type)
64 – 1.7E308 ~ 1.7E308
char (character type)
16
In computer science, a data type is a type that defines how data is stored in memory and what operations can be performed on it. The above content shows the ranges and characteristics of different data types, including integer, floating – point, and character – type data. These data types are crucial in programming as they determine the types and sizes of data that variables can store.
~ – boolean (boolean type)
true or false
02 Detailed Explanation of Integer Types
▲ Overview of Integer Types
When data does not contain decimals or fractions, it can be declared as an integer variable. It should be noted that in Java, the range of integer types is not related to the machine on which the Java code runs. Long – type values have a suffix of L or l, such as 1234568L; hexadecimal values have a prefix of 0X or 0x, such as 0xCAFE; and octal values have a prefix of 0, but their use is generally not recommended. Starting from Java 7, binary numbers can be written with the 0b or 0B prefix, for example, 0b101 represents 9. In addition, numeric literals can have underscores (such as 1_000_000 representing one million), but the Java compiler will remove these underscores.
In C and C++, the sizes of data types such as int and long can vary depending on the target platform. However, Java does not have unsigned forms of int, long, short, and byte types. When using integer – type values, it is important to pay attention to the range of the integer type, because the default value of the integer type is the int type.
03 Detailed Explanation of Floating – Point Types
▲ Overview of Floating – Point Types
Floating – point types are used to represent values that contain a fractional part. In Java, the default type of a floating – point value is double, which can be suffixed with D or d, or have no suffix. On the other hand, float – type values must be suffixed with F or f.
In addition, in the hexadecimal notation of floating – point values, p is used to represent the exponent, so floating – point numbers can be represented in hexadecimal form. However, it should be noted that due to possible rounding errors in floating – point numbers, they are not suitable for application scenarios requiring high precision such as financial calculations. In these cases, the BigDecimal class can be considered for precise calculations.
04 Character and Boolean Types
▲ Character Type
The char type occupies two bytes and is mainly used to store English letters and various characters. In Java, literals of the char type must be enclosed in single quotes (”). You can assign numeric or character – type values to character variables, and char – type values can also be represented in hexadecimal form, ranging from \u0000 to \Uffff.
In addition, Java provides a set of special escape sequences to represent some characters that are difficult to input directly. These escape characters are preprocessed before parsing the code. For example, \n represents a newline character, \t represents a tab character, and \u0022 represents a double – quote character. The use of these escape characters makes it more convenient for us to handle and input various special characters.
▲ Boolean Type
Variables of the boolean type can only take two values: true and false. This type of variable is often used to control the execution flow of a program.
05 Default Values of Primitive Data Types
In Java, if a variable is declared without an explicit assignment, the system will assign a default value to the variable. However, for local variables, an initial value must be assigned at the time of declaration; otherwise, a compilation error will occur. The following is an overview of the default values of primitive data types in Java:
Data Type | Default Value
——–|——-
Boolean type | false
Integer type | 0
Floating – point type | 0.0
Character type | null (The default value of the character type is actually represented by the Unicode null character, i.e., ‘\u0000’)
String type | null
Please note that for the boolean type, its default value is false; for numeric types such as integer and floating – point types, the default value is 0 or 0.0. Understanding the default values of primitive data types in Java helps to avoid compilation errors, including that the default values of character and string types are both null. When writing Java code, understanding these default values helps to avoid potential errors and exceptions.