File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why immutable wrapper objects are not considered as compile time constant? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why immutable wrapper objects are not considered as compile time constant?" Watch "why immutable wrapper objects are not considered as compile time constant?" New topic
Author

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

Karthikeyan Kandasamy
Greenhorn

Joined: Apr 17, 2011
Posts: 14
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

Joined: Aug 06, 2012
Posts: 333

Compiler does compile time stuff.See more.


OCA7
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

interesting infact!
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4392
    
    8

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

Joined: Jan 28, 2008
Posts: 5575

so *I am not sure*, but i say in bottom layer compiler is runtime .
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4392
    
    8

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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Apr 17, 2011
Posts: 14
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

Joined: Jan 28, 2008
Posts: 5575

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why immutable wrapper objects are not considered as compile time constant?