• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple switch question (Java OCA 8 Programmer I Study Guide, Sybex)

 
Ben Pittens
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

See code very simple switch statement:

Why I can't use a Integer object in the switch? Line 9 will not compile.
If I use a final Integer in a case I get a compile error like: "case expressions must be constant expressions"

If I use the int bcd variable it works. (Is commented out).

If I look in the B & S book(page 73) it should work.


 
Roel De Nijs
Sheriff
Posts: 10662
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
Ben Pittens wrote:Why I can't use a Integer object in the switch? Line 9 will not compile.

Because you can only use compile-time constants. An object can NEVER be a compile-time constant. A compile-time constant (or a constant expression) can only be a primitive data type or String. In this topic you'll find an explanation about what's a compile-time constant.
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the book it says about the Data types in supported by <expression> in switch(expression) and not about case statements.
case statements can take only constant expressions and compile time constants
This is from the JLS
Every case label has a case constant,
which is either a constant expression or the name of an enum constant. Switch
labels and their case constants are said to be associated with the switch statement.

In your code your variable "bcd" works because it is final. and As Roel says Integer is not a compile time constant so your variable "a" doesnt compile.Integer variable or primitive variable used in case statements will not work as below.
 
Ben Pittens
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel and Ramya,

Thanks for your support and infomation I understand this now.

Kind Regards
Ben
 
Roel De Nijs
Sheriff
Posts: 10662
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
Some additional topics worth reading:
  • in this topic you'll find some illustrative code snippets about to be a compile-time constant or not to be a compile-time constant
  • this topic has also some examples about the switch statement
  • here is another one about trying to use StringBuilder in a switch statement
  • this topic is about autoboxing of primitive variables (and sometimes it matters to have a compile-time constant to avoid a compiler error)


  • Hope it helps!
    Kind regards,
    Roel

    PS. Ramya, have a cow for an excellent post with an illustrative code snippet and reference to the JLS!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic