1 Star 0 Fork 1

beside/Java

forked from LiuYan/Java 
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
InstanceofOptimization.java 925 Bytes
Copy Edit Raw Blame History
liuyan authored 2023-06-30 16:27 +08:00 . code refactor
/**
* @author LiuYan
* @date 2023/5/16
*/
public class InstanceofOptimization {
public static void main(String[] args) {
// Java8 instanceof
final String oldStr8;
Object oldObj = "java8";
if (oldObj instanceof String) {
String str = (String) oldObj;
oldStr8 = str.toUpperCase();
} else {
oldStr8 = "";
}
System.out.println("Java8 instanceof:" + oldStr8);
// Java14 instanceof 运算符检查 obj 是否是 String 类型的实例,如果是,则使用强制类型转换将 obj 转换为 String 对象,并在之后的代码中使用该对象
final String newStr14;
Object newObj = "java14";
if (newObj instanceof String s) {
newStr14 = s.toUpperCase();
} else {
newStr14 = "";
}
System.out.println("Java14 instanceof:" + newStr14);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/beside/java.git
git@gitee.com:beside/java.git
beside
java
Java
master

Search