• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

case statement

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello all;I want to know about case statement use.Please check below two examples:
egA:

in this case a is constant expression and it is ok.but in this egB:

at that time compiler error for case a.why?I think Integer a is also final and it is constant too.Please explain me.Thank alot.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See this previous topic on what is a compile time constant... https://coderanch.com/t/454384/java/java/compile-time-constant

then... if you think that it is still a compile time constant, we can further discuss.

Henry
 
saw mon
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:See this previous topic on what is a compile time constant... https://coderanch.com/t/454384/java/java/compile-time-constant

then... if you think that it is still a compile time constant, we can further discuss.

Henry



Dear Henry,When I read the link you gave me ,then I saw final variables with primitives and strings value are time constant.For this case ,I think is that final Integer a = 10,and a is auto unboxed to primitive types.But It shows compiler error and It cannot be auto unboxed to primitives type.Right?
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Saw, as you read correctly, only primitives and Strings are compile time constants. You can only use literals and compile time constants in case expression. Since the Integer wrapper is not compile time constant, you can't use it in case expression even if it can be auto unboxed to primitive int...
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Furthermore, if you read the link again, regarding constant expressions, you will notice that method calls can't be involved in constant expressions. This means that with method calls, it will no longer be a compile time constants. And autoboxing, are converted to method calls to box/unbox between the wrapper and its primitive, by the compiler.

Henry
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Funny, this.

You can have an Integer in the switch() statement but not in a case statement.
This is ok:


 
reply
    Bookmark Topic Watch Topic
  • New Topic