• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Static

 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Folks

Following code is from K&B:-

1. class Ring {
2. final static int x2 = 7;
3. final static Integer x4 = 8;
4. public static void main(String[] args) {
5. Integer x1 = 5;
6. String s = "a";
7. if(x1 < 9) s += "b";
8. switch(x1) {
9. case 5: s += "c";
10. case x2: s += "d";
11. case x4: s += "e";
12. }
13. System.out.println(s);
14. }
15. }

"The answer is compilation fails due to line 11. Because a switch statement requires its case expressions to be constants, and wrapper variables (even final static ones) aren�t considered constants. The rest of the code is correct."

My question is if line 11 is causing a Compilation error then line 10 should also cause for same error. But in answer they are only considering line 11 . It make me confuse.

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

At line 10 the case expression is a constant, its a final static int as opposed to line 11 which is a final static Integer.

So line 10 does not cause a compilation error.Its case expression is a constant .
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Gaurav says,
My question is if line 11 is causing a Compilation error then line 10 should also cause for same error. But in answer they are only considering line 11 . It make me confuse.



x2 is an int constant, x4 is a wrapper constant. Wrapper object can't be
treated as a constant in the case. Although wrapper objects are immutable(that can't be changed) but where primitive constant (enum as well ) is required, wrapper can't be used.



Regards,
cmbhatt
 
Gaurav Pavan Kumar Jain
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Megha & Chandra



 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

final static Integer x4 = 8;

The compilation error is due to this statement.It doesnot compile and we
get the error "Type mismatch;cannot convert from int to Integer".

We can't assign an int value directly to an Integer type.
It can be done like this:

Integer x4=new Integer(8);

But it can't be used as a case constant.

Thanks and Regards,
Anish
 
Blueberry pie is best when it is firm and you can hold in your hand. Smell it. And smell this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic