首页 > 其他 > 详细

多种数据类型混合运算

时间:2021-09-01 17:09:38      阅读:29      评论:0      收藏:0      [点我收藏+]

/*
多种数据类型做混合运算的时候,最终的结果类型是"最大容量"对应的类型。

char+short+byte 这个除外。
因为char + short + byte混合运算的时候,会各自先转换成int再做运算。
*/
public class a
{
  public static void main(String[] args){

    long a = 10L;
    char c = ‘a‘;
    short s = 100;
    int i = 30;

    //求和
    System.out.println(a + c + s + i);

    // 错误:不兼容的类型:从long转换到int可能会有损失
    // 计算结果是long类型
    // int x = a + c + s + i;

    int x = (int)(a + c + s + i);
    System.out.println(x);

    // 以下程序执行结果是?
    // java中规定,int类型和int类型最终的结果还是int类型。
    int temp = 10 / 3; // / 是除号
    System.out.println(temp); // 3

    int temp2 = 1 / 2;

    System.out.println(temp2); // 0

  }
}

多种数据类型混合运算

原文:https://www.cnblogs.com/874162650-com/p/15203248.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!