强制类型转换,又称为造型(cast),用于强制转换数据类型,有可能损失精度。
通过 (type)var 的语法,将值从原始类型转换为特定类型。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class TestCastConvert { public static void main(String[] args) { double a = 3.14; int b = (int)a; System.out.println(b);
int c = 97; char d = (char)c; System.out.println(d);
int e = 300; byte f = (byte)e; System.out.println(f); } }
|