package com.xch.test04;
/**
* 笔试题测试
*
* @author XuChenghe
* @date 2023/8/31 16:03
*/
public class Test08 {
public static void main(String[] args) {
f1();
System.out.println();
f2();
System.out.println();
f3();
}
/**
* 计算公式优先级
*/
public static void f1() {
int x = 10;
System.out.println(x += x -= x *= x);
// 运算符优先级一致,则按结合性(计算顺序)从右到左,运算前y赋值后不会随计算而变化(始终为10)
int y = 10;
System.out.println(y += (y -= (y *= y)));
System.out.println("y = " + y); // 最终回填y=-80
// 原义拆分
int z = 10;
int z1 = z * z;
System.out.println(z1); // 100
int z2 = z - z1;
System.out.println(z2); // -90
int z3 = z + z2;
System.out.println(z3); // -80
z = z3;
System.out.println("z = " + z); // 最终回填z=-80
// 分部运算(前后赋值)
int w = 10;
int w1 = w -= (w *= w);
System.out.println("w1 = " + w1);
System.out.println("w = " + w); // 前段运算回填w=-90
System.out.println(w += w1);
System.out.println("w = " + w); // -180
}
/**
* 运算符运算
*/
public static void f2() {
// 且[有一个为假即为假](true时要执行后面判断,false时不要执行后面判断)
System.out.println(true && true); // true
System.out.println(true && false); // false
System.out.println(false && true); // false
System.out.println(false && false); // false
// 或[有一个为真即为真](true时不要执行后面判断,false时要执行后面判断)
System.out.println(true || true); // true
System.out.println(true || false); // true
System.out.println(false || true); // true
System.out.println(false || false); // false
// 按位非(取反)
// 3 = 0000 0011
// ~3 = 1111 1100
// = 1111 1011(转为十进制,先-1)
// = -0000 0100(反转为4,所以原值为-4)
// = -4
System.out.println(~3); // -4
// 按位与[结果和&&一致](前面是true/false都会执行后面的逻辑)
System.out.println(1 & 1); // 1
System.out.println(1 & 0); // 0
System.out.println(0 & 1); // 0
System.out.println(0 & 0); // 0
// 按位或[结果和||一致](前面是true/false都会执行后面的逻辑)
System.out.println(1 | 1); // 1
System.out.println(1 | 0); // 1
System.out.println(0 | 1); // 1
System.out.println(0 | 0); // 0
// 按位异或[不同时为1,相同时为0](前后都要判断)
System.out.println(1 ^ 1); // 0
System.out.println(1 ^ 0); // 1
System.out.println(0 ^ 1); // 1
System.out.println(0 ^ 0); // 0
}
/**
* 运算符执行
*/
public static void f3() {
String str = null;
// 后面要报错则str要为null,&&为true才判断后面逻辑,逆向思维所以前面str==null则会触发异常
// System.out.println(str == null && str.length() == 0); // 报错NLP
System.out.println(str != null && str.length() == 0);
// 后面要报错则str要为null,||为false才判断后面逻辑,逆向思维所以前面str!=null则会触发异常
System.out.println(str == null || str.length() == 0);
// System.out.println(str != null || str.length() == 0); // 报错NLP
// 前面是true/false都会判断后面逻辑
// System.out.println(str == null & str.length() == 0); // 报错NLP
// System.out.println(str != null & str.length() == 0); // 报错NLP
// 前面是true/false都会判断后面逻辑
// System.out.println(str == null | str.length() == 0); // 报错NLP
// System.out.println(str != null | str.length() == 0); // 报错NLP
}
}
笔试题-计算公式优先级+运算符运算+运算符执行
于 2023-08-31 18:06:55 首次发布