Java数据类型转换

类型的转换
  自动转换
   如果数字运算都是以int去处理,但是如果有一个数字是long,就会转换成long
   byte和short都会以int类型去运算
   
   byte short -->int
   int  long  -->long
   byte short int long -->long
   
   
  强制转换
   (数据类型)
   如果浮点数转成整型  直接把小数点后面的全部去掉,没有四舍五入
   强转的时候默认强转紧跟在后面的变量,如果需要强转整个那就把要强转的括起来
   double d = 12.5;
   int a = (int)d;
   
  float 的存储的数据范围 大于 long

public class Test01Calc{
 
 public static void main(String[] args){
  
  //同类型的运算
  int i1 = 9/5;
  double d1 = 9.0/5;
  //将来做int/int计算的时候,如果要保留小数,可以提前*1.0
  double d2 = 81*1.0/20;
  
  //byte b1 = 1;
  //b1 = b1+1;
  //b1++;
  
  byte b1 = 1;
  byte b2 = 1;
  //也会丢失精度
  byte b3 = (byte)(b1+b2);
  
  System.out.println(b3);
  
  
  int i2 = 100;
  float f2 = i2+1.0f;
  
  float f3 = 100.0f;
  int i3 = (int)f3+1;
  
  long l4 = 21000000000L;
  float f4 = l4+1.0f;
  
  double d5 = 12.8;
  int i5 = (int)d5;
  
  double d6 = Math.random();
  int score = (int)(d6*101);
  
  System.out.println(score);
  
 }
 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值