Fetch the repository succeeded.
This action will force synchronization from LiuYan/Java, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
/**
* @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);
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。