Java学习——Java运算符
位运算符
A = 0011 1100 B = 0000 1101 ----------------- A&b = 0000 1100 A | B = 0011 1101 A ^ B = 0011 0001
A << 2 = 1111 0000
A >>> 2 = 0000 1111 ~A= 1100 0011
例子
package import_test; public class Employee {public static void main(String args[]){int a = 60; int b = 13; System.out.println(a | b); System.out.println(a & b); System.out.println(a ^ b); System.out.println(~a); System.out.println(a << 2); System.out.println(a >>> 2); } }
结果
61 12 49 -61 240 15
条件运算符
public class Employee { public static void main(String args[]){ int a = 10; int b = 8; String rev = a > b ? "a > b" : "a<= b"; System.out.println(rev); } }
结果
a > b
instanceOf运算符
public class Employee { public static void main(String args[]){ Employee eg = new Employee(); String s = "String"; System.out.println(s instanceof String); System.out.println(eg instanceof Employee); //boolean b = s instanceof Employee); //System.out.println(b); } }
结果
true true