004.运算符 Operator

第四讲

  1. 当有若干个变量参与运算时,结果类型取决于这些变量中表示范围最大的那个变量类型。

比如:参与运算的变量中,有整型 int , 有双精度浮点型double,有短整型short,那么最后的结果类型就是double。

public class test{
    public static void main (String[] args){
        int a = 1;
        int b = 2;
        
        int c = a + b;//3
        int d = a - b;//-1
        int e = a/b;//0
        int f = a*b;//2
        System.out.println(c); 
        System.out.println(d); 
        System.out.println(e); 
        System.out.println(f); 
    }
}
    public static void main (String[] args){
        double a = 1;//出现double都让路
        int b = 2;

        double c = a + b;
        double d = a - b;
        double e = a/b;
        double f = a*b;
        System.out.println(c);
        System.out.println(d);
        System.out.println(e);
        System.out.println(f);
    }
}

下面的代码中,a 和 b 都是整型,但是通过(double)a这种转换将a转换为一个匿名的变量,该变量的类型是double,但是要注意:a本身依旧是int类型,而不是double类型,这样。(double)a/b就是double类型除以int类型,结果自然是double类型。

    public ststic void main (String[] args){
        int a = 1;
        int b = 2;
        double e = (double) a/b;
        System.out.println(e);
    }
}

取模运算符:使用%表示。【取模的规律:取模的结果符号永远与被除数的符号相同】

public class text{
   public static void main (String[] args){
       int a = 5;
       int b = 3;
       int c = a % b;
       System.out.println (c);
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值