Java是一種高級(jí)編程語言,使用操作符和運(yùn)算符來處理變量和數(shù)值。Java中的操作符和運(yùn)算符包括算術(shù)運(yùn)算符、位運(yùn)算符、關(guān)系運(yùn)算符、邏輯運(yùn)算符和條件運(yùn)算符。
//算術(shù)運(yùn)算符 int a = 10; int b = 20; int c = a + b; //加法 int d = a - b; //減法 int e = a * b; //乘法 int f = b / a; //除法 int g = b % a; //取模 //位運(yùn)算符 int h = 3; // 二進(jìn)制為00000011 int i = 6; // 二進(jìn)制為00000110 int j = h & i; //按位與 00000010 int k = h | i; //按位或 00000111 int l = h ^ i; //按位異或 00000101 int m = ~h; //按位非 11111100 int n = i<< 2; //左移兩位 00011000 int o = i >>2; //右移兩位 00000001 //關(guān)系運(yùn)算符 int p = 5; int q = 10; boolean r = p == q; //等于 boolean s = p != q; //不等于 boolean t = p >q; //大于 boolean u = p< q; //小于 boolean v = p >= q; //大于等于 boolean w = p<= q; //小于等于 //邏輯運(yùn)算符 boolean x = true; boolean y = false; boolean z = x && y; //與 boolean a1 = x || y; //或 boolean a2 = !x; //非 //條件運(yùn)算符 int max = (p >q) ? p : q;
以上就是Java中常用的操作符和運(yùn)算符,熟練掌握這些運(yùn)算符可以提高編程效率和代碼質(zhì)量。