Chuyển đổi范围和
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| casting/ˈkæstɪŋ/ | ép kiểu |
| truncates/ˈtrʌŋkeɪts/ | cắt phần thập phân |
| widening/ˈwaɪdənɪŋ/ | mở rộng (kiểu) |
| overflow/ˌəʊvəˈfləʊ/ | vượt quá giới hạn |
| narrowing/ˈnærəʊɪŋ/ | thu hẹp (kiểu) |
| range/reɪndʒ/ | phạm vi |
Changing a value's type
- Casting 类型转换 converts a value from one type to another.
- Write the target type in parentheses:
(double) 5gives5.0;(int) 3.9gives3. - Casting to
inttruncates 截断 — it drops the decimal part, it does not round. (int) 3.9is3, and(int) -2.7is-2(toward zero).
Widening happens automatically
- Going from a smaller type to a larger one (
int→double) is widening 拓宽. - Widening is safe, so Java does it automatically:
double d = 5;stores5.0. - No information is lost going from
inttodouble. - You rarely need to write a widening cast, but it's allowed.

Casting truncates toward zero; int arithmetic overflows 溢出 past Integer.MAX_VALUE
Narrowing needs an explicit cast
- Going from larger to smaller (
double→int) is narrowing 收窄 — it can lose data. - Java requires an explicit cast:
int n = (int) 3.9;(without the cast, it won't compile). - The cast is you promising "I know I'm dropping the decimals."
- Narrowing truncates toward zero.
Range and overflow
- Each type has a fixed range 取值范围:
intholds roughly ±2.1 billion. - Exceeding the range causes overflow — the value wraps around to a wrong number.
- No error is raised; the result is just silently wrong.
- For huge whole numbers, a bigger type (like
long) is needed.
Casting to int truncates — it does NOT round. (int) 3.9 is 3, not 4; (int) 2.99 is 2. To force floating-point division, cast one operand: (double) 7 / 2 is 3.5, but (int)(7.0 / 2) is 3. And watch overflow: an int result beyond ±2.1 billion wraps silently, with no error.
Averaging two ints correctly:
int a = 7, b = 2;int avg = a / b;→3(int division drops the remainder — probably not what you want).double avg = (double) a / b;→3.5(casting one side forces decimal division).
Casting (type) value converts between types. int → double (widening) is automatic and safe; double → int (narrowing) needs an explicit cast and truncates toward zero (no rounding). Each type has a fixed range; exceeding it causes silent overflow.
Chuyển đổi giữa int và double
(int) cắt cụt 3.9 thành 3; chuyển đổi một vế cho ra 3.5.
Giá trị của (int) 3.9 trong Java 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
Chuyển đổi sang int cắt cụt (bỏ phần thập phân) → 3, không phải 4.
Chuyển đổi int sang double (một chuyển đổi an toàn, tự động) được gọi là...
Độ 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
Nhỏ → lớn là mở rộng, được thực hiện tự động.
Cái nào cần chuyển đổi rõ ràng để biên dịch?
Độ 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
double → int là thu hẹp và yêu cầu chuyển đổi rõ ràng.
Giá trị của (double) 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
Việc ép một toán hạng thành số thực buộc phép chia số thực → 3.5.
Kết quả int vượt quá phạm vi cho phép gây tràn, và Java báo cáo điều này như một lỗi.
Độ 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
Tràn số wraps (quay vòng) im lặng — không có lỗi nào được nObserver.