Biến và Kiểu dữ liệu
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| variable/ˈveərɪəbl/ | biến |
| primitive types/ˈprɪmɪtɪv taɪps/ | kiểu nguyên thủy |
| reference types/ˈrefrəns taɪps/ | kiểu tham chiếu |
A variable is a labelled box
- A variable 变量 is a named place in memory that stores a value.
- Give it a type, a name, and (usually) a starting value:
int score = 0; - Read it or change it by name; the box holds the latest value.
- Every variable in Java must be declared with a type before use.
The primitive types
- Java has eight primitive types 基本类型; four matter most for AP:
int— whole numbers (e.g.42).double— decimals (e.g.3.14).boolean—trueorfalse.char— a single character ('A').- Primitives store the actual value directly in the box.

Java's primitive types int, double and boolean, plus char, and String (an object)
Reference types
- Non-primitive types (like
String) are reference types 引用类型. - A reference variable stores the address of an object, not the object itself.
String name = "Ada";—namepoints to a String object.- Primitive vs. reference is a distinction you'll use all course.
Naming and declaring
- Names start with a letter; use camelCase (
highScore,numStudents). - Declaring reserves the box; initializing gives it a first value.
- A
finalvariable is a constant — its value can't change after it's set. - Choose clear names — code is read far more than it's written.
int and double are different types — mind the division. int division throws away the remainder: 7 / 2 is 3, not 3.5. To get 3.5, at least one operand must be a double (7.0 / 2). Storing a decimal in an int won't even compile without a cast. Pick the type that matches the values you need.
Tracing a few declarations:
int radius = 5;→ the boxradiusholds5.double pi = 3.14159;→piholds3.14159.radius = 10;→ the same box now holds10(the old5is gone).
A variable is a typed, named storage box. The core primitive types are int, double, boolean, and char, which store a value directly; reference types like String store the address of an object. Declare with a type, use camelCase, and remember int division drops the remainder.
Theo dõi giá trị biến
Bước từng bước để xem mỗi ô lấy giá trị của nó.
Kiểu nào lưu số nguyên như 42?
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
int lưu số nguyên; double lưu số thập phân.
Trong Java, giá trị của biểu thức int 7 / 2 là gì?
Độ khó 2/5 · 140 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Phép chia int bỏ phần dư: 7/2 = 3.
Một biến lưu địa chỉ của đối tượng (không phải giá trị của nó) có một...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
String và các đối tượng khác là kiểu tham chiếu.
Kiểu đặt tên chuẩn Java nào cho biến?
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Biến sử dụng camelCase trong Java.
Biến được khai báo final có thể thay đổi giá trị sau này.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
final khiến nó thành hằng — giá trị không thể thay đổi.