我正在 eclipse 中使用 Java 时收到一项警告,上面写着“dead code”。这是什么意思?
在 Java 中永远不会到达并且在程序生命周期内永远不会被执行的代码块/语句被称为不可到达块/语句。
public class UnreachableCodeExample {
public String greet() {
System.out.println("This is a greet method ");
return "Hello";
System.out.println("This is an unreachable code ");
}
public static void main(String args[]) {
new UnreachableCodeExample().greet();
}
}死代码
死代码是一种不可到达的代码,但它不会产生编译时错误。但是,如果你在 eclipse 中执行它,它会给你一个警告。
示例
在以下 Java 程序中
public class UnreachableCodeExample {
public void greet() {
System.out.println("This is the greet method ");
if(true){
return;
}
System.out.println("This is a dead code ");
}
public static void main(String args[]) {
new UnreachableCodeExample().greet();
}
}输出
This is the greet method
如果你在 eclipse 中运行相同的程序,它会像下面一样发出警告:

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP