💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## [运算符总结](https://lingcoder.gitee.io/onjava8/#/book/04-Operators?id=%e8%bf%90%e7%ae%97%e7%ac%a6%e6%80%bb%e7%bb%93) 上述示例分别向我们展示了哪些基本类型能被用于特定的运算符。基本上,下面的代码示例是对上述所有示例的重复,只不过概括了所有的基本类型。这个文件能被正确地编译,因为我已经把编译不通过的那部分用注释`//`过滤了。代码示例: ~~~ // operators/AllOps.java // 测试所有基本类型的运算符操作 // 看看哪些是能被 Java 编译器接受的 public class AllOps { // 布尔值的接收测试: void f(boolean b) {} void boolTest(boolean x, boolean y) { // 算数运算符: //- x = x * y; //- x = x / y; //- x = x % y; //- x = x + y; //- x = x - y; //- x++; //- x--; //- x = +y; //- x = -y; // 关系运算符和逻辑运算符: //- f(x > y); //- f(x >= y); //- f(x < y); //- f(x <= y); f(x == y); f(x != y); f(!y); x = x && y; x = x || y; // 按位运算符: //- x = ~y; x = x & y; x = x | y; x = x ^ y; //- x = x << 1; //- x = x >> 1; //- x = x >>> 1; // 联合赋值: //- x += y; //- x -= y; //- x *= y; //- x /= y; //- x %= y; //- x <<= 1; //- x >>= 1; //- x >>>= 1; x &= y; x ^= y; x |= y; // 类型转换: //- char c = (char)x; //- byte b = (byte)x; //- short s = (short)x; //- int i = (int)x; //- long l = (long)x; //- float f = (float)x; //- double d = (double)x; } void charTest(char x, char y) { // 算数运算符: x = (char)(x * y); x = (char)(x / y); x = (char)(x % y); x = (char)(x + y); x = (char)(x - y); x++; x--; x = (char) + y; x = (char) - y; // 关系和逻辑运算符: f(x > y); f(x >= y); f(x < y); f(x <= y); f(x == y); f(x != y); //- f(!x); //- f(x && y); //- f(x || y); // 按位运算符: x= (char)~y; x = (char)(x & y); x = (char)(x | y); x = (char)(x ^ y); x = (char)(x << 1); x = (char)(x >> 1); x = (char)(x >>> 1); // 联合赋值: x += y; x -= y; x *= y; x /= y; x %= y; x <<= 1; x >>= 1; x >>>= 1; x &= y; x ^= y; x |= y; // 类型转换 //- boolean bl = (boolean)x; byte b = (byte)x; short s = (short)x; int i = (int)x; long l = (long)x; float f = (float)x; double d = (double)x; } void byteTest(byte x, byte y) { // 算数运算符: x = (byte)(x* y); x = (byte)(x / y); x = (byte)(x % y); x = (byte)(x + y); x = (byte)(x - y); x++; x--; x = (byte) + y; x = (byte) - y; // 关系和逻辑运算符: f(x > y); f(x >= y); f(x < y); f(x <= y); f(x == y); f(x != y); //- f(!x); //- f(x && y); //- f(x || y); //按位运算符: x = (byte)~y; x = (byte)(x & y); x = (byte)(x | y); x = (byte)(x ^ y); x = (byte)(x << 1); x = (byte)(x >> 1); x = (byte)(x >>> 1); // 联合赋值: x += y; x -= y; x *= y; x /= y; x %= y; x <<= 1; x >>= 1; x >>>= 1; x &= y; x ^= y; x |= y; // 类型转换: //- boolean bl = (boolean)x; char c = (char)x; short s = (short)x; int i = (int)x; long l = (long)x; float f = (float)x; double d = (double)x; } void shortTest(short x, short y) { // 算术运算符: x = (short)(x * y); x = (short)(x / y); x = (short)(x % y); x = (short)(x + y); x = (short)(x - y); x++; x--; x = (short) + y; x = (short) - y; // 关系和逻辑运算符: f(x > y); f(x >= y); f(x < y); f(x <= y); f(x == y); f(x != y); //- f(!x); //- f(x && y); //- f(x || y); // 按位运算符: x = (short) ~ y; x = (short)(x & y); x = (short)(x | y); x = (short)(x ^ y); x = (short)(x << 1); x = (short)(x >> 1); x = (short)(x >>> 1); // Compound assignment: x += y; x -= y; x *= y; x /= y; x %= y; x <<= 1; x >>= 1; x >>>= 1; x &= y; x ^= y; x |= y; // 类型转换: //- boolean bl = (boolean)x; char c = (char)x; byte b = (byte)x; int i = (int)x; long l = (long)x; float f = (float)x; double d = (double)x; } void intTest(int x, int y) { // 算术运算符: x = x * y; x = x / y; x = x % y; x = x + y; x = x - y; x++; x--; x = +y; x = -y; // 关系和逻辑运算符: f(x > y); f(x >= y); f(x < y); f(x <= y); f(x == y); f(x != y); //- f(!x); //- f(x && y); //- f(x || y); // 按位运算符: x = ~y; x = x & y; x = x | y; x = x ^ y; x = x << 1; x = x >> 1; x = x >>> 1; // 联合赋值: x += y; x -= y; x *= y; x /= y; x %= y; x <<= 1; x >>= 1; x >>>= 1; x &= y; x ^= y; x |= y; // 类型转换: //- boolean bl = (boolean)x; char c = (char)x; byte b = (byte)x; short s = (short)x; long l = (long)x; float f = (float)x; double d = (double)x; } void longTest(long x, long y) { // 算数运算符: x = x * y; x = x / y; x = x % y; x = x + y; x = x - y; x++; x--; x = +y; x = -y; // 关系和逻辑运算符: f(x > y); f(x >= y); f(x < y); f(x <= y); f(x == y); f(x != y); //- f(!x); //- f(x && y); //- f(x || y); // 按位运算符: x = ~y; x = x & y; x = x | y; x = x ^ y; x = x << 1; x = x >> 1; x = x >>> 1; // 联合赋值: x += y; x -= y; x *= y; x /= y; x %= y; x <<= 1; x >>= 1; x >>>= 1; x &= y; x ^= y; x |= y; // 类型转换: //- boolean bl = (boolean)x; char c = (char)x; byte b = (byte)x; short s = (short)x; int i = (int)x; float f = (float)x; double d = (double)x; } void floatTest(float x, float y) { // 算数运算符: x = x * y; x = x / y; x = x % y; x = x + y; x = x - y; x++; x--; x = +y; x = -y; // 关系和逻辑运算符: f(x > y); f(x >= y); f(x < y); f(x <= y); f(x == y); f(x != y); //- f(!x); //- f(x && y); //- f(x || y); // 按位运算符: //- x = ~y; //- x = x & y; //- x = x | y; //- x = x ^ y; //- x = x << 1; //- x = x >> 1; //- x = x >>> 1; // 联合赋值: x += y; x -= y; x *= y; x /= y; x %= y; //- x <<= 1; //- x >>= 1; //- x >>>= 1; //- x &= y; //- x ^= y; //- x |= y; // 类型转换: //- boolean bl = (boolean)x; char c = (char)x; byte b = (byte)x; short s = (short)x; int i = (int)x; long l = (long)x; double d = (double)x; } void doubleTest(double x, double y) { // 算术运算符: x = x * y; x = x / y; x = x % y; x = x + y; x = x - y; x++; x--; x = +y; x = -y; // 关系和逻辑运算符: f(x > y); f(x >= y); f(x < y); f(x <= y); f(x == y); f(x != y); //- f(!x); //- f(x && y); //- f(x || y); // 按位运算符: //- x = ~y; //- x = x & y; //- x = x | y; //- x = x ^ y; //- x = x << 1; //- x = x >> 1; //- x = x >>> 1; // 联合赋值: x += y; x -= y; x *= y; x /= y; x %= y; //- x <<= 1; //- x >>= 1; //- x >>>= 1; //- x &= y; //- x ^= y; //- x |= y; // 类型转换: //- boolean bl = (boolean)x; char c = (char)x; byte b = (byte)x; short s = (short)x; int i = (int)x; long l = (long)x; float f = (float)x; } } ~~~ **注意**:**boolean**类型的运算是受限的。你能为其赋值`true`或`false`,也可测试它的值是否是`true`或`false`。但你不能对其作加减等其他运算。 在**char**,**byte**和**short**类型中,我们可以看到算术运算符的“类型转换”效果。我们必须要显式强制类型转换才能将结果重新赋值为原始类型。对于**int**类型的运算则不用转换,因为默认就是**int**型。虽然我们不用再停下来思考这一切是否安全,但是两个大的 int 型整数相乘时,结果有可能超出**int**型的范围,这种情况下结果会发生溢出。下面的代码示例: ~~~ // operators/Overflow.java // 厉害了!内存溢出 public class Overflow { public static void main(String[] args) { int big = Integer.MAX_VALUE; System.out.println("big = " + big); int bigger = big * 4; System.out.println("bigger = " + bigger); } } ~~~ 输出结果: ~~~ big = 2147483647 bigger = -4 ~~~ 编译器没有报错或警告,运行时一切看起来都无异常。诚然,Java 是优秀的,但是还不足够优秀。 对于**char**,**byte**或者**short**,混合赋值并不需要类型转换。即使为它们执行转型操作,也会获得与直接算术运算相同的结果。另外,省略类型转换可以使代码显得更加简练。总之,除**boolean**以外,其他任何两种基本类型间都可进行类型转换。当我们进行向下转换类型时,需要注意结果的范围是否溢出,否则我们就很可能在不知不觉中丢失精度。