• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Shantanu Sardal
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.
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • 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
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • 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.

 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3820
10
  • 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: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • 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
C++ Linux Python
  • 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: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • 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
C++ Linux Python
  • 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: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic