Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Chapter 2 : Swtich statement error? (Java OCA 8 Programmer I Study Guide)

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Chapter 2, on page 74, line 1.

While explaining the allowed compile time constant values for switch statements, the book says that final constant must be declared and initialized on the same line. This is not true. The final constant can be initialized on another line.
 
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
Hi Shantanu Sardal,

First of all, a warm welcome to CodeRanch!

Shantanu Sardal wrote:While explaining the allowed compile time constant values for switch statements, the book says that final constant must be declared and initialized on the same line. This is not true. The final constant can be initialized on another line.


You are correct about a final constant: it can be initialized on another line. But then you do not have a compile-time constant. And the case labels have to be literals or compile-time constants; otherwise the code won't compile. So the study guide is spot-on and it's not an error.

The above is illustrated in this code snippetAlthough variable by1 is a (final) constant, it's not a compile-time constant and therefore it's illegal to use it as a case label.

Hope it helps!
Kind regards,
Roel
 
Ranch Hand
Posts: 789
Python C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The OCAJP has come a long way from:

The following will compile:


True
False



For better or worse I don't know.

 
Enthuware Software Support
Posts: 4451
41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Guillermo Ishi wrote:The OCAJP has come a long way from:

The following will compile:


True
False



For better or worse I don't know.


Not completely true. You may not necessarily see a simple True/False question but you will be tested on the signature of the main method (including order of keywords). You may see this code as one of the options.
 
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

Guillermo Ishi wrote:The OCAJP has come a long way from:

The following will compile:


True
False


I have never seen a true/false question on a (Sun/Oracle) certification exam: not when I took SCJP 1.4 in 2005, nor when taking OCAJP7 at the beginning of this year. And that makes sense, because the exam format is multiple choice questions (which this question clearly isn't).

A possible question could be "Select 3 valid declarations of the main method" and then such declaration is one of the possible options (one you should not select if you want to answer that question correctly ). And of course this main method can be used in a code snippet which seems to test you on some complex loop logic or on ArrayList, but is actually testing you on a valid main method declaration.

Kind regards,
Roel
 
Guillermo Ishi
Ranch Hand
Posts: 789
Python C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I remember some true/falses from pre-2014 ocajp but I wouldn't swear to it.
Two line final is different from one line final? If you can't fix it, feature it, as my poor old mentor said.
 
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

Guillermo Ishi wrote:Two line final is different from one line final? If you can't fix it, feature it, as my poor old mentor said.


Of course it is! One is a compile-time constant, the other isn't.

And even one line final differs from one line final
 
Guillermo Ishi
Ranch Hand
Posts: 789
Python C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically speaking I guess you could put the whole program on the same line. Book should say "compile time" instead of "same line".
 
Roel De Nijs
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

Guillermo Ishi wrote:Technically speaking I guess you could put the whole program on the same line.


Yes, you could write your whole program on the same line if you really want to. It could get tricky if you use single-line comments.
 
No matter how many women are assigned to the project, a pregnancy takes nine months. Much longer than this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic