你是否也经历过这些崩溃瞬间?
i++
和++i
的区别都说不清 a==b
和equals()
的区别",大脑突然空白 🚀 这个系列就是为你打造的Java「速效救心丸」!
我们承诺:
✅ 每天1分钟:地铁通勤、午休间隙即可完成学习
✅ 直击痛点:只讲高频考点和实际开发中的「坑位」
✅ 拒绝臃肿:没有冗长概念堆砌,每篇都有可运行的代码标本
明日预告:《控制流程:if-else条件语句实战》
// 算术运算
int result = 10 + 3 * 5; // 结果=25(不是65!)
double pi = Math.PI % 3; // 取模运算的特殊用法
// 比较运算
boolean flag = (new Integer(127) == 127); // 自动拆箱陷阱
// 逻辑运算
String user = null;
if (user != null && user.length() > 0) { // 短路运算避免NPE
System.out.println("Valid user");
}
***
# Python中**优先级高于位运算
print(2 ** 3 & 1) # 输出0 → (8) & 1
// Java中位运算符优先级混乱
System.out.println(2 * 3 & 1); // 输出0 → (6) & 1
System.out.println(8 ^ 3 << 1); // 输出22 → 8^(6)
开发警示:Java的<<
优先级低于算术运算符,混合使用时建议显式加括号
// JMH基准测试结果(ns/op)
@BenchmarkMode(Mode.AverageTime)
public class IncrementBenchmark {
@Benchmark
public void prefix() {
for (int i = 0; i < 1_000_000; ++i) {}
}
@Benchmark
public void postfix() {
for (int i = 0; i < 1_000_000; i++) {}
}
}
// 测试结果差异 <1%(现代JVM已优化)
结论:性能差异可忽略,代码可读性优先
***
Object result = (short)1 + (byte)2; // 实际类型是int!
System.out.println(result.getClass()); // class java.lang.Integer
String s1 = new String("Java");
String s2 = "Java";
System.out.println(s1 == s2); // false
System.out.println(s1.equals(s2)); // true
Integer a = null;
int b = true ? a : 0; // 抛出NPE
***
public void processOrder(Order order) {
// 使用短路与避免深层null检查
if (order != null
&& order.getUser() != null
&& order.getUser().getAddress() != null) {
// 安全操作
}
}
public class MoneyUtil {
// 使用分作为基本单位避免浮点误差
public static int add(int centsA, int centsB) {
return centsA + centsB;
}
public static String format(int cents) {
return cents / 100 + "." + cents % 100;
}
}
***
单算移关与,异或逻条赋
(谐音:单身算一关,鱼饵逻辑调副)
解释:单目→算术→移位→关系→按位与→异或→按位或→逻辑与→逻辑或→三目→赋值
a > b?
/ \
true false
| |
c != d? 整个表达式=false
/ \
true false
| |
整个 整个
表达式=true 表达式=false
***
byte a = 10;
short b = 20;
int c = a + b; // 查看字节码:
iload_1 // 加载byte类型变量(实际转为int)
iload_2 // 加载short类型变量(转为int)
iadd // 执行int相加
istore_3 // 存储结果
本质揭示:JVM运算最小单位是int类型,所有小于int的类型运算都会发生隐式类型转换
if (a() && b()) { ... }
对应字节码:
invokestatic a()Z
ifeq falseLabel // 如果a()为false直接跳转
invokestatic b()Z
ifeq falseLabel
***
int x = 10 + 20 * 3;
// 编译后直接变为:
int x = 70;
现代编译器会将:
for (int i=0; i<100; i++) {}
优化为:
for (int i=0; i<100; ++i) {}
boolean isPowerOfTwo = (n & (n - 1)) == 0;
a ^= b;
b ^= a;
a ^= b;
int rgb = 0xFF0099;
int red = (rgb >> 16) & 0xFF; // 0xFF
int green = (rgb >> 8) & 0xFF; // 0x00
int blue = rgb & 0xFF; // 0x99
// 使用位运算快速定位多个哈希位置
long[] bits = new long[1024];
int hash1 = ...;
int hash2 = ...;
bits[hash1 >> 6] |= (1L << (hash1 & 0x3F));
bits[hash2 >> 6] |= (1L << (hash2 & 0x3F));
// 原始代码
void demo() {
int i = 0;
i++;
++i;
}
// javap -c 反编译结果
0: iconst_0
1: istore_1
2: iinc 1, 1 // i++ 直接修改局部变量表
5: iinc 1, 1 // ++i 同样优化为iinc指令
洞见:现代JVM已优化前缀/后缀自增的底层实现,性能差异更多存在于理论层面
***
@Configuration
@ConditionalOnExpression(
"#{T(java.lang.Math).random() > 0.5} " +
"&& '${server.env}' == 'prod'"
)
public class RiskControlConfig {
// 动态启用配置
}
<if test="age != null and age > 18">
AND status = 'ADULT'
</if>
<!-- 必须同时判空与比较,避免NPE -->
***
// 错误现象
System.out.println(0.1 + 0.2 == 0.3); // false
// 调试方案
BigDecimal a = new BigDecimal("0.1");
BigDecimal b = new BigDecimal("0.2");
System.out.println(a.add(b).equals(new BigDecimal("0.3"))); // true
// 编译错误示例
Object o = true ? "hello" : 123;
// 实际类型是Serializable接口类型!
***
***
🚫 在商业计算中使用float/double\
🚫 在复杂表达式中混合使用不同类运算符\
🚫 在循环条件中使用带有副作用的表达式
✅ 金额计算统一使用BigDecimal或分存储\
✅ 多运算符表达式显式使用括号\
✅ 逻辑判断前进行防御性空检查
***
***
// 终极挑战:预测输出结果
public class OperatorQuiz {
public static void main(String[] args) {
int x = 5;
x += x++ * ++x / x--;
System.out.println(x); // 答案见文末
}
}
(答案:通过反编译分析可得最终结果,此处暂不揭晓以鼓励读者实践)
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。