Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Maybe "unreachable code" should be "dead code" in page 70, (Java OCA 8 Programmer I Study Guide)

 
Ranch Hand
Posts: 221
27
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read from some of Roel's posts that unreachable code is different from dead code. One of them:

Roel De Nijs wrote:Very important: dead code is not the same as unreachable code! If your code has unreachable code, you'll get a compiler error; if your code has dead code, the code compiles successfully.



On page 70, the second print statement of example maybe is a dead code according to this manner
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mushfiq Mammadov wrote:I read from some of Roel's posts that unreachable code is different from dead code. One of them:

Roel De Nijs wrote:Very important: dead code is not the same as unreachable code! If your code has unreachable code, you'll get a compiler error; if your code has dead code, the code compiles successfully.


This is of course true (otherwise I would not have posted such a statement ), but it's only from the compiler's point of view! In the code snippet on page 70, the authors just want to explain that if hourOfDay is less than 11, you'll never get "Good Morning" printed on the console, it will always be "Good Afternoon" because (as stated in the study guide) the order of the conditions is very important. So from that perspective the code is unreachable (and thus pretty useless). But that's from a developer's or logical point of view, not the compiler's point of view. Let's look at what the compiler thinks about this code next.

In this code snippetline1 is unreachable from a developer's point of view. From the compiler's point of view: line1 is not dead code, nor unreachable code. So the above code will compile successfully without warnings and errors!

If you mark the local variable hourOfDay as final, it's another story.Now the compiler indicates all code after the closing curly brace of the 1st if statement as dead code. So that means the code will compile successfully with warnings! Thus from the compiler's point of view, all this code is considered to be dead code

Hope it helps!
Kind regards,
Roel
 
Mushfiq Mammadov
Ranch Hand
Posts: 221
27
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote: This is of course true (otherwise I would not have posted such a statement ), but it's only from the compiler's point of view! In the code snippet on page 70, the authors just want to explain that if hourOfDay is less than 11, you'll never get "Good Morning" printed on the console, it will always be "Good Afternoon" because (as stated in the study guide) the order of the conditions is very important. So from that perspective the code is unreachable (and thus pretty useless). But that's from a developer's or logical point of view, not the compiler's point of view.


Everything is clear now. Thanks a lot, Roel

Roel De Nijs wrote: line1 is unreachable from a developer's point of view. From the compiler's point of view: line1 is not dead code, nor unreachable code. So the above code will compile successfully without warnings and errors!


At first glance it seems simple, but it is not such as

Roel De Nijs wrote:Now the compiler indicates all code after the closing curly brace of the 1st if statement as dead code. So that means the code will compile successfully with warnings! Thus from the compiler's point of view, all this code is considered to be dead


Do IDE inform us about warnings?
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mushfiq Mammadov wrote:

Roel De Nijs wrote:Now the compiler indicates all code after the closing curly brace of the 1st if statement as dead code. So that means the code will compile successfully with warnings! Thus from the compiler's point of view, all this code is considered to be dead


Do IDE inform us about warnings?


An IDE normally shows the compiler warnings as well. And when you are actually developing production code, it can provide much more warnings about your code style (e.g. unused private data members).
 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!