File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dead Code on return statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dead Code on return statement" Watch "Dead Code on return statement" New topic
Author

Dead Code on return statement

Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

your program compiles fine for me and the output is null4

SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Dead code isn't a Compilation Error.

Wiki Definition : Dead code is a computer programming term for code in the source code of a program which is executed but whose result is never used in any other computation.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
I separated the code for more clarity. I am trying to the difference between these two programs


Prasad Kharkar wrote:your program compiles fine for me and the output is null4
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Saibabaa Pragada : The Compiler doesn't know anything about the logic of the program, it checks every possible ways.
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
This is where I am getting confused..In the Example1.java file, there are 2 return statements, Second return statement is giving Dead Code warning, In Example2.java, we have 2 return statements. second return is giving compiler error.
anirudh jagithyala
Ranch Hand

Joined: Dec 07, 2010
Posts: 41
In case of Example1
the foo() declaration defines that a string must be returned, but if(true) is always true is known by the second phase(semmantic analysis) of compilation so the warning occurs....{but in the first phase of compiler the syntax is checked only}

In case of second Example2
the declaration defines similary to return a String and each and every case returns a String , and moreover due to the default statement atleast one case excutes,, any case that executes returns a String So correspondingly the compiler works...

Hope i had justified my answer for your question
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

in the second example
you have written return statement for every case statement
hence the compiler knows for sure that control will be returned from the switch case and hence
the code below the switch will not be executed at all
hence return statement below the switch case gives error


hope this helps
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dead Code on return statement
 
Similar Threads
Method Overloading with default param values
main() is the entry point
Question on Inheritance syntax
SCJP6 book page 298 errata
For Loop Doubt