String 转换成 int :
String num = "-2";
int x =-2;
if(x == Integer.parseInt(num))
{
}
使用了
Integer.parseInt();如果不使用转换将报错:Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Incompatible operand types int and String
short 类型可以转换为 long类型。但是如果Long转换成short类型会丢失数据,且需要使用cast运算符强制转换:
long y =40002;
short x = (short) y;
System.out.println(x);
//输出结果-25534
从float和double型转换为int型或short型。会直接截断小数位,只保留整数位。