强制类型转换 类型转换可缩小值的范围 int a A、 long b b= a a=(int)b;把ong型变量b强制转换为int型 int x365 byte y=(byte)x;把int型变量制转换为be型 √·∥强制转换后丢失一部分数据,使得y的值为100。 般使用强制类型转换可能会导致数值溢出或精度的下降,应尽量避免。 点类型转换为整数类型将使所有小数 部分丢失
强制类型转换 • 类型转换可缩小值的范围 例: int a ; long b ; b = a ; a = ( int ) b ; /*把long型变量b强制转换为int型*/ int x=365; byte y=(byte)x; /*把int型变量i强制转换为byte型*/ • // 强制转换后丢失一部分数据,使得y的值为100。 一般使用强制类型转换可能会导致数值溢出或精度的下降,应尽量避免。 • 浮点类型转换为整数类型将使所有小数 部分丢失
public class Age public static void main(string args[D) Int age Years int age days long age Seconds age YearsInteger parseInt(args[oD age days=age Years 365 age Seconds=(age Years"365*24L 6060) System. out. print( You are"+age Days+days old. System. out. print( You are+age Seconds+'seconds old 查看结果; Age. java
public class Age { public static void main(String args[ ]) { int ageYears; int ageDays; long ageSeconds; ageYears=Integer.parseInt(args[0]); ageDays=ageYears*365; ageSeconds=(ageYears*365*24L*60*60); System.out.print(“You are”+ageDays+”days old.”); System.out.print(“You are”+ageSeconds+”seconds old.”); } } 查看结果:Age.java