运算符

JAVA总结(运算符)

JAVA总结(运算符)

注意:

 % 符号的右边的值不能为0;

自增、自减运算符:是一种特殊的算数运算符,在算数运算符中需要两个操作数,而自增自减运算符就是一个操作数;

(++a;--a)这种形式的自增自减运算,先计算自增、自减,再进行表达式运算

(a++;a--)先进行表达式运算,再进行自增、自减运算

两种形式的相同之处就是执行结束后都会加a或减a;

public static void main(String[] args) {
int i = 1;
int j = 2;
System.out.println("加法:"+(i+j));
System.out.println("减法:"+(i-j));
System.out.println("乘法:"+(i*j));
System.out.println("除法:"+(i/j));
System.out.println("取余:"+(i%j));
System.out.println("自加法:"+(++i));
System.out.println("自减法:"+(--i));
System.out.println("自加法:"+(j++));
System.out.println("自减法:"+(j--));
System.out.println("自减后:"+j);
}

加法:3
减法:-1
乘法:2
除法:0
取余:1
自加法:2
自减法:1
自加法:2
自减法:3
自减后:2


JAVA总结(运算符)

注意:

“==”与equals()的区别:

“==”比较的是两个对象的地址

equals()比较的是2个对象的内容

( ==是比较两个基本类型的值是否相等,equals()是比较两个对象是否相等。


public static void main(String[] args) {
int i = 10;
int j = 10;
int c = 20;
System.out.println("i==j = "+(i == j));
System.out.println("i==c = "+(i == c));
System.out.println("!i = " + (i != j));
System.out.println("i>j = "+(i > j));
System.out.println("i<j = "+(i < j));
System.out.println("i>=j = "+(i >= j));
System.out.println("i<=j = "+(i <= j));
}

i==j = true
i==c = false
!i = false
i>j = false
i<j = false
i>=j = true
i<=j = true


JAVA总结(运算符)

JAVA总结(运算符)

public static void main(String[] args) {
int i = 10;
int j = 10;
int c = 20;
System.out.println("i|j = "+(i | j));
System.out.println("i^c = "+(i ^ c));
System.out.println("~i = " + (~ i));
System.out.println("i<<j = "+(i << j));
System.out.println("i>>j = "+(i >> j));
System.out.println("i>>>j = "+(i >>> j));
}


i|j = 10
i^c = 30
~i = -11
i<<j = 10240
i>>j = 0
i>>>j = 0

JAVA总结(运算符)

public static void main(String[] args) {
boolean b = true;
boolean c = false;

System.out.println("c&&b = "+(b&&c));
System.out.println("c||b = "+(c||b));
System.out.println("!c = "+(!c));
}

c&&b = false
c||b = true
!c = true


JAVA总结(运算符)

public static void main(String[] args) {
int a = 10;
    int b = 20;
    int c = 0;
    c = a + b;
    System.out.println("c = a + b = " + c );
    c += a ;
    System.out.println("c += a  = " + c );
    c -= a ;
    System.out.println("c -= a = " + c );
    c *= a ;
    System.out.println("c *= a = " + c );
    a = 10;
    c = 15;
    c /= a ;
    System.out.println("c /= a = " + c );
    a = 10;
    c = 15;
    c %= a ;
    System.out.println("c %= a  = " + c );
    c <<= 2 ;
    System.out.println("c <<= 2 = " + c );
    c >>= 2 ;
    System.out.println("c >>= 2 = " + c );
    c >>= 2 ;
    System.out.println("c >>= a = " + c );
    c &= a ;
    System.out.println("c &= 2  = " + c );
    c ^= a ;
    System.out.println("c ^= a   = " + c );
    c |= a ;
    System.out.println("c |= a   = " + c );
}

c = a + b = 30
c += a  = 40
c -= a = 30
c *= a = 300
c /= a = 1
c %= a  = 5
c <<= 2 = 20
c >>= 2 = 5
c >>= a = 1
c &= 2  = 0
c ^= a   = 10
c |= a   = 10

JAVA总结(运算符)

JAVA总结(运算符)

相关文章:

  • 2021-07-14
  • 2022-02-10
  • 2021-04-03
  • 2021-11-23
  • 2021-07-15
  • 2022-12-23
  • 2021-04-12
  • 2021-07-06
猜你喜欢
  • 2021-12-06
  • 2022-12-23
  • 2021-05-08
  • 2021-12-23
  • 2021-06-29
相关资源
相似解决方案