In Java programming, data types are like “labels” we attach to data, telling the compiler what type the data is and how to process it. Java’s data types are mainly divided into primitive data types and reference data types. Today, we’ll focus on three of the most basic and commonly used data types: the integer type int, the boolean type boolean, and the string type String.
一、整数类型 int¶
int (integer type) is the most commonly used integer data type in Java, used to store integers without a fractional part (such as age, scores, quantities, etc.).
Characteristics:¶
- Occupies 4 bytes (32 bits) and is a signed integer. Its value range is from
-2^31(approximately -2.1 billion) to2^31 - 1(approximately 2.1 billion), i.e.,-2147483648to2147483647. - If the value exceeds this range, an “overflow” error will occur, causing the program to fail.
Declaration and Assignment:¶
Declare a variable using the int keyword and assign an integer value using the assignment operator =:
int age = 18; // Age assigned as 18
int score = 95; // Score assigned as 95
int number = -10; // Negative numbers are also allowed
Example Code:¶
public class IntDemo {
public static void main(String[] args) {
int myScore = 88;
int myAge = 20;
// Print variable values
System.out.println("My score is: " + myScore); // Output: My score is: 88
System.out.println("My age is: " + myAge); // Output: My age is: 20
// Calculate and print the result
int sum = myScore + myAge;
System.out.println("Sum of score + age: " + sum); // Output: Sum of score + age: 108
}
}
Notes:¶
intcan only store integers. Assigning a decimal (e.g.,int num = 3.14;) will cause an error (decimal numbers must be stored indoubletype).- Variable names cannot be Java keywords (such as
int,class, etc.) and cannot start with a number (e.g.,1numis invalid).
二、布尔类型 boolean¶
boolean (boolean type) has only two fixed values: true (true) and false (false), mainly used to represent the result of conditional judgments (such as “whether passed” “whether the exam was passed”, etc.).
Characteristics:¶
- No concept of positive/negative, only represents logical “true” or “false”.
- Often used with conditional statements like
ifandwhileto control program execution flow.
Declaration and Assignment:¶
Declare a variable using the boolean keyword; assignment can only be true or false:
boolean isPass = true; // Exam passed
boolean hasBook = false; // No book
Example Code:¶
public class BooleanDemo {
public static void main(String[] args) {
boolean isStudent = true;
// Use if condition to judge
if (isStudent) {
System.out.println("You are a student and need to learn Java!");
} else {
System.out.println("You are not a student, but you can still learn Java!");
}
// Common mistake: Cannot assign 1 or 0!
// boolean invalid = 1; // Error! Must use true/false
}
}
Notes:¶
- Assignment must be
trueorfalse(case-sensitive; cannot be written asTrueorFalse). - Cannot be directly converted with numbers (e.g.,
true≠ 1,false≠ 0).
三、字符串类型 String¶
String (string type) is the most commonly used reference data type (not a primitive type) in Java, used to store text information (such as names, addresses, sentences, etc.).
Characteristics:¶
- Stores a “character sequence” and must be enclosed in double quotes
""(single quotes''represent a single character of typechar, not a string). - Internally, it is an instance of the Java class (
java.lang.String), and its content cannot be directly modified after assignment (reassignment is required if modification is needed).
Declaration and Assignment:¶
Directly assign text by enclosing it in double quotes:
String name = "Zhang San"; // Name
String message = "Hello Java"; // Message text
Example Code:¶
public class StringDemo {
public static void main(String[] args) {
// Declare string variables
String username = "Li Si";
String school = "Java Programming Academy";
// Print the string
System.out.println("Name: " + username); // Output: Name: Li Si
// Concatenate strings
String info = username + " studies at " + school;
System.out.println(info); // Output: Li Si studies at Java Programming Academy
// Note: Strings cannot be directly modified!
// username[0] = 'Wang'; // Error! String is immutable; reassign instead:
username = "Wang Wu"; // Reassignment is allowed
}
}
Notes:¶
- Text must be enclosed in double quotes; otherwise, it will be treated as a variable or cause an error (e.g.,
String name = Zhang San;will report an error; must be written as"Zhang San"). - Strings can be concatenated using the
+operator or processed with methods of theStringclass (e.g.,length()to get the length).
Summary¶
int, boolean, and String are the most basic data types in Java, used for handling integers, logical judgments, and text information, respectively. Mastering their declaration, assignment, and usage is fundamental to writing Java programs. In subsequent learning, we’ll encounter more data types and complex operations, but these three types will be used throughout daily programming.