Casting and Range · 类型转换与范围
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| casting/ˈkæstɪŋ/ | 类型转换 | lèi xíng zhuǎn huàn |
| truncates/ˈtrʌŋkeɪts/ | 截断 | jié duàn |
| widening/ˈwaɪdənɪŋ/ | 拓宽 | tuò kuān |
| narrowing/ˈnærəʊɪŋ/ | 收窄 | shōu zhǎi |
| range/reɪndʒ/ | 取值范围 | qǔ zhí fàn wéi |
| overflow/ˌəʊvəˈfləʊ/ | 溢出 | yì chū |
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).
改变值的类型
- 类型转换把一个值从一种类型转成另一种。
- 把目标类型写在圆括号里:
(double) 5得到5.0;(int) 3.9得到3。 - 转换成
int会截断——它丢掉小数部分,不四舍五入。 (int) 3.9是3,(int) -2.7是-2(朝零方向)。
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.
拓宽会自动发生
- 从较小类型到较大类型(
int→double)是拓宽。 - 拓宽是安全的,所以 Java 自动进行:
double d = 5;存的是5.0。 - 从
int到double不会丢失信息。 - 你很少需要写拓宽转换,但允许这么写。
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.
收窄需要显式转换
- 从较大到较小(
double→int)是收窄——它可能丢失数据。 - Java 要求显式转换:
int n = (int) 3.9;(没有转换就无法编译)。 - 这个转换是你在承诺“我知道我在丢掉小数”。
- 收窄朝零方向截断。
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.
范围与溢出
- 每种类型都有固定的范围:
int大约能存 ±21 亿。 - 超出范围会造成溢出——值会绕回到一个错误的数字。
- 不会抛出错误;结果只是悄悄地错了。
- 对巨大的整数,需要更大的类型(如
long)。
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.
转换成 int 是截断——它不四舍五入。(int) 3.9 是 3,而非 4;(int) 2.99 是 2。要强制浮点除法,转换一个操作数:(double) 7 / 2 是 3.5,但 (int)(7.0 / 2) 是 3。并留意溢出:超过 ±21 亿的 int 结果会悄悄绕回,没有任何错误。
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).
正确地对两个 int 求平均:
int a = 7, b = 2;int avg = a / b;→3(int 除法丢掉余数——大概不是你想要的)。double avg = (double) a / b;→3.5(转换一侧强制小数除法)。
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.
类型转换 (type) value 在类型之间转换。int → double(拓宽)自动且安全;double → int(收窄)需要显式转换,并朝零方向截断(不四舍五入)。每种类型有固定范围;超出会造成悄悄的溢出。
Casting between int and double · 在 int 与 double 之间转换
(int) truncates 3.9 to 3; casting one side gives 3.5. · (int) 把 3.9 截断为 3;转换一侧得到 3.5。
What is the value of (int) 3.9 in Java? · 在 Java 中 (int) 3.9 的值是多少?
Casting to int truncates (drops decimals) → 3, not 4. · 转换成 int 会截断(丢掉小数)→ 3,而非 4。
Converting int to double (a safe, automatic conversion) is called... · 把 int 转成 double(一种安全、自动的转换)叫做……
Small → large is widening, done automatically. · 小 → 大 是拓宽,自动完成。
Which needs an explicit cast to compile? · 哪一个需要显式转换才能编译?
double → int is narrowing and requires the explicit cast. · double → int 是收窄,需要显式转换。
What is the value of (double) 7 / 2? · (double) 7 / 2 的值是多少?
Casting one operand forces floating-point division → 3.5. · 转换一个操作数强制浮点除法 → 3.5。
An int result beyond its range causes overflow, which Java reports as an error. · 超出范围的 int 结果会造成溢出,Java 会把它报告为一个错误。
Overflow wraps silently — no error is raised. · 溢出会悄悄绕回——不会抛出错误。