运算符

运算符

一、算术运算符

代码:

public class Deom0 {
    public static void main(String[] args) {
        //算术(二元)运算符 ①
        int a=10;
        int b=20;
        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/(double)b);
    }
}
public class Demo1 {
    public static void main(String[] args) {
        //算术(二元)运算符 ②
        long a=2511635292L;
        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型 默认计算结果int
    }
}


运行结果:

30
-10
200
0.5
2511635433
141
18
二、关系运算符

代码:

public class Demo2 {
    public static void main(String[] args) {
        //关系运算符返回的结果:true false 布尔值
        int a=10;
        int b=20;
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
    }
}

运行结果:

false
true
false
三、逻辑运算符

代码:

public class Demo4 {
    public static void main(String[] args) {
        //逻辑运算符:与(&&) 或(||)  非(!)
        boolean a=true;
        boolean b=false;
        System.out.println("a && b: "+(a&&b));//逻辑与运算:两个变量都为真,结果才为true
        System.out.println("a || b: "+(a||b));//逻辑或运算:两个变量有一个为真,结果就为true
        System.out.println("!(a && b): "+(!(a && b)));//逻辑非运算:如果为真,则变假;如果为假,则变为真
    }
}
}

运行结果:

a && b: false
a || b: true
!(a && b): true
四、位运算符

代码:

/**
 * 位运算符:& | ^ ~ >> << >>>
 */

public class Demo5 {
    public static void main(String[] args) {
        /*
        A=0011 1100
        B=0000 1101
        A&B=0000 1100   两个都为1才为1,其余皆为0
        A|B=0011 1101   只要有一个为1就为1
        A^B=0011 0001   相同为0,不同为1
        ~B =1111 0010   1变0 0变1
        -------------------------------------------
        位运算符效率极高!!
        3<<4=3*2^4=48   左移
        16>>2=16/2^2=4  右移

        原理:
        0000 0000       0
        0000 0001       1
        0000 0010       2
        0000 0011       3
        0000 0100       4
        0000 1000       8
        0001 0000       16
        -------------------------------------------
         */
        System.out.println(3<<4);
        System.out.println(16>>2);
    }
}

运行结果:

48
4
五、一元运算符

代码:

public class Demo3 {
    public static void main(String[] args) {
        //一元运算符:自增 自减  ++  --
        int a=3;
        int b=a++;//先给b赋值,再自增
        System.out.println("a="+a);
        System.out.println("b="+b);
        int c=++a;//先自增,再赋值
        System.out.println("a="+a);
        System.out.println("c="+c);
    }

运行结果:

a=4
b=3
a=5
c=5
六、三元运算符

代码:

public class Demo6 {
    public static void main(String[] args) {
        //三元运算符:  x ?a : b    如果x为true,则结果为a,否则结果为b
        int score=90;
        String result=score<60? "不及格":"及格";
        System.out.println(result);
    }
}

运行结果:

及格
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值