Приведение типов и диапазоны
Введено| English | Значение |
|---|---|
| casting/ˈkæstɪŋ/ | приведение типов |
| truncates/ˈtrʌŋkeɪts/ | обрезание |
| widening/ˈwaɪdənɪŋ/ | расширение (типа) |
| overflow/ˌəʊvəˈfləʊ/ | переполнение |
| narrowing/ˈnærəʊɪŋ/ | сужение (типа) |
| range/reɪndʒ/ | диапазон значений |
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.
Приведение между int и double
(int) усекло 3.9 до 3; приведение одной стороны дает 3.5.
Чему равно значение (int) 3.9 в Java?
Сложность 2/5 · 140 монет + небольшой бонус за первое выполнение сегодня
Приведение к int отсекает (отбрасывает дробную часть) → 3, а не 4.
Преобразование int в double (безопасная автоматическая конвертация) называется...
Сложность 1/5 · 100 монет + небольшой бонус за первое выполнение сегодня
Маленький → большой — это расширение, происходит автоматически.
Что требует явного приведения для компиляции?
Сложность 1/5 · 100 монет + небольшой бонус за первое выполнение сегодня
double → int — это сужение и требует явного приведения.
Чему равно значение (double) 7 / 2?
Сложность 2/5 · 140 монет + небольшой бонус за первое выполнение сегодня
Приведение одного операнда к типу с плавающей точкой вызывает деление с плавающей точкой → 3.5.
Результат типа int, выходящий за пределы его диапазона, вызывает переполнение, которое Java отображает как ошибку.
Сложность 1/5 · 100 монет + небольшой бонус за первое выполнение сегодня
Переполнение происходит молча — ошибка не выбрасывается.