- 最終更新
- 2026/08/17
3秒でわかる
値をどの型として扱うかを明示し直す操作。整数どうしの割り算で小数が消える、といった型の食い違いによる計算間違いを防ぐために使います。
30秒図解
もう少し詳しく
どういうものか
キャストは、(int) のように
言語にas は
なぜ必要か
型が
具体例
int total = 7;
int count = 2;
System.out.println(total / count); // 3 切り捨てられる
System.out.println((double) total / count); // 3.5 片方を実数にする
double d = 3.9;
System.out.println((int) d); // 3 切り捨て。四捨五入ではない
Object o = "こんにちは";
String s = (String) o; // 参照型のキャスト
System.out.println(s.length()); // 5つまずきやすいところ
大きい(int) で(byte) で
キャストを(double)(total / count) は
参照型のキャストは、as はundefined を
覚え方
キャストは


