This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why immutable wrapper objects are not considered as compile time constant?

 
Karthikeyan Kandasamy
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While going through rules for Case expressions in K&B book, its mentioned that only integer literal values and compile time constants are allowed.

I couldn't figure out why integer wrappers are not allowed in Case expressions. In the below example, variable i2 is final and it refers to Integer wrapper object (immutable). So compiler could easily use the value (200) instead of object.




Thanks
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compiler does compile time stuff.See more.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interesting infact!
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karthikeyan Kandasamy wrote:I couldn't figure out why integer wrappers are not allowed in Case expressions.

They could have been, but the language designers never bothered. You'd have to ask them for the real reason. My speculation, though - they never found a good reason to. Can you think of a situation where it would be genuinely useful?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so *I am not sure*, but i say in bottom layer compiler is runtime .
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:so *I am not sure*, but i say in bottom layer compiler is runtime .

Sorry, but what do you mean? "compiler is runtime" doesn't make any sense to me.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Sorry, but what do you mean? "compiler is runtime" doesn't make any sense to me.
s
Um. I mean javac call java .. but you are right though..
 
Karthikeyan Kandasamy
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for tour responses.

@Matthew Brown, I don't have any real life requirement to use wrapper as case expression.

Thanks.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am extremely sorry for my previous post mess .

From JLS:

Every case constant expression associated with a switch statement must be assignable (ยง5.2) to the type of the switch Expression.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic