java基础运算符01

shiweirui / 2023-08-17 / 原文

运算符

  • 算数运算符:+,-,*,/,%(取余),++(自增),--(自减)
  • 赋值运算符:=
  • 关系运算符:<,>,>=,<=,==(两个=是等于,一个=是赋值),!=instanceof
  • 逻辑关系运算符:&&,||,!
  • 位运算符:&,|,^,~,>>,<<,>>>
  • 条件运算符:?:
  • 扩展赋值运算符:+=,-=,*=,/=在运算中,各个数值类型取等级最高的,如果有long类型取long类型,long之下是double,之后是int,如:
package base1;

public class Demo {
    public static void main(String[] args) {
        long l = 121212121212L;
        int i = 100 ;
        short s = 20;
        byte b = 5;
        System.out.println(l+i+s+b);
        System.out.println(i+s+b);
        System.out.println(s+b);

    }
}

输出结果:

121212121337
125
25

Process finished with exit code 0
  • 关系运算符返回的值为正确与错误,也就是true和false是布尔值,例:
package base1;

public class Demo {
    public static void main(String[] args) {
        long l = 121212121212L;
        int i = 100 ;

        System.out.println(l>i);
        System.out.println(l==i);
        System.out.println(l<i);
        System.out.println(l!=i);

    }
}

输出结果为:

true
false
false
true

Process finished with exit code 0
  • 取余,又称模运算:
package base1;

public class Demo {
    public static void main(String[] args) {
        int a = 9;
        int b = 4;

        System.out.println(a%b);


    }
}

输出结果为:

1

Process finished with exit code 0