File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dead Code on return statement

 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your program compiles fine for me and the output is null4
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saibabaa Pragada : The Compiler doesn't know anything about the logic of the program, it checks every possible ways.
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic