java中的操作符

java中的操作符

一、基础操作符

注意除法,要想得到正确的结果,需要进行类型转换。

package operator;

public class Demo1 {
    public static void main(String[] args) {
        int a =10;
        int b =20;
        int c =25;
        int d =25;
        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/b);//0
        System.out.println((double)a/b);//0.5
        System.out.println(a/(double)b);//0.5
    }
}

不同的数据类型相加,结果的数据类型为混合类型中最高类型。如果没有int类型,结果还是为int类型,例如c+d。

package operator;

public class Demo2 {
    public static void main(String[] args) {
        long a = 121413125432532L;
        int b = 123;
        short c = 10;
        byte d = 8;
        System.out.println(a+b+c+d);//long
        System.out.println(b+c+d);//int
        System.out.println(c+d);//int
        //有一个为long,加起来结果就为long
        //没有long,无论有没有int,比如c+d,结果还是为int
        //自动转为混合类型中的最高类型
    }
}

二、++,-- !!!

  • b=++a:先自加,后赋值。
  • c=a++:先赋值,再自加。
package operator;
public class Demo3 {
    public static void main(String[] args) {
        // ++  -- 运算符
        int a = 5;
        int b = a++;//先 b=a ,再a=a+1。此时a=6,b=5

        System.out.println(a);//6
        System.out.println(b);//5

        int c = ++a ;//先 a=a+1,再 c=a。此时a=7,c=7

        System.out.println(a);//7
        System.out.println(b);//5
        System.out.println(c);//7


        //幂运算
        double pow = Math.pow(2, 3);
        System.out.println(pow);//8.0


    }
}


三、逻辑运算符

&&、||、!(两位)

值得注意的是:&&逻辑与,当前一个表达式为假,不再计算后一个

package operator;

public class Demo4 {
    public static void main(String[] args) {
        // &&逻辑与
        //当前一个为假,不再计算后一个
        int a = 5;
        boolean b = (a<4)&&(a++<4);
        System.out.println(a);//5
        System.out.println(b);//fase
        //a++<4 没有计算,a依旧为5
    }
}

四、位运算符

&、|、^(一位)

值得注意的是,左移相当于*2,右移相当于/2,效率极高。

五、连接符 :+

package operator;

public class Demo5 {
    public static void main(String[] args) {
        //字符串连接符   +  String
        int a = 10;
        int b = 20;
        System.out.println(a+b+"");//30  计算结果之后拼接上字符串
        System.out.println(""+a+b);// 1020 当字符串在前面,将后面的全都转化为字符串
    }
}

六、条件运算符

三元运算符 x ? y : z
如果x为真,结果为y,否则结果为z。

package operator;

public class Demo6 {
    //三元运算符
    //x ? y : z
    // 如果x为真,结果为y,否则结果为z
    public static void main(String[] args) {

        int score = 60;
        String type = score >= 60 ? "及格": "不及格";
        System.out.println(type);//及格
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值