long的变量后面没有L加会有什么后果

不加L的话,默认就是int型了。
当给long赋值一个超过int范围的值的时候,会出问题。

java中对字面的数值是以int的形式来表示的
 例如:long l= 6553555522222
报错:The literal 6553555522222 of type int is out of range 

 

不加L默认是int,int转为long是安全的,所以会自动转,能编译通过
float浮点数不加F默认是double类型,double转float可能损失精度,因为不会自动转,编译是通不过的

posted @ 2018-11-12 11:29  twoheads  阅读(1962)  评论(0编辑  收藏  举报