- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
While working with Java in eclipse I got a warning saying "dead code". What does it mean?
A code block/statement in Java to which the control never reaches and never gets executed during the lifetime of the program is known as unreachable block/statement.
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(); } }
Dead code
A dead code is an unreachable code, but it doesn’t generate compile time error. But if you execute it in eclipse it gives you a warning.
Example
In the following Java program
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(); } }
Output
This is the greet method
If you run the same program in eclipse it raises a warning as −
Advertisements