运算符
注意:
% 符号的右边的值不能为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
注意:
“==”与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
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
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
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