• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

using switch statement

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

incompatible types
found : char
required: java.lang.Integer
case 'a' :
^
1 error

but when i replace the //case 'a'// with case 1,the there is no complie time error

2.

this code also runs fine with no error
shouldn't there be error at line //case wo//
as in
incompatible types
found : int
required: java.lang.Character
???

[/code]


3. i am studying from scjp 6 by kathy sierra.
In the fifth chapter they have discussed about switch statement but there is nowhere mentioned that braces can be used in the case constants in a switch statement.
can anybody refer me a link where can i find some good material regarding Switch
 
Ranch Hand
Posts: 310
1
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this code, it compiles fine (YOUR FIRST CODE) . The issue with your code was duplicate 'case' statements
You had case w: and case 'a', which are same because you declared final int w = 'a';

Your SECOND code is fine as it should be because you can use a final int in case: So "case wo: " compiles fine!!
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajeev Rnair wrote:Try this code, it compiles fine (YOUR FIRST CODE) . The issue with your code was duplicate 'case' statements
You had case w: and case 'a', which are same because you declared final int w = 'a';

Your SECOND code is fine as it should be because you can use a final int in case: So "case wo: " compiles fine!!



Does the above code work?

You can't wide and box...



This will work...
 
Rajeev Rnair
Ranch Hand
Posts: 310
1
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
switch(new Integer(4)) {} works fine. The new Integer() unboxes to int. So there is no widening and boxing here
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajeev Rnair wrote:switch(new Integer(4)) {} works fine. The new Integer() unboxes to int. So there is no widening and boxing here



Please check my coding...
 
Rajeev Rnair
Ranch Hand
Posts: 310
1
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Please check my coding...


Your code is fine too, there is no problem with that!
 
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
This shouldn't be that hard to understand. The JLS says that the case values must be assignable to the switch expression. So all you need to do is try this

In the fifth chapter they have discussed about switch statement but there is nowhere mentioned that braces can be used in the case constants in a switch statement.


Braces represent a block of statements. You can use them anywhere where there are statements. This has nothing to do with switch-case. I can even write this
 
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajeev Rnair wrote:Try this code, it compiles fine (YOUR FIRST CODE) . The issue with your code was duplicate 'case' statements
You had case w: and case 'a', which are same because you declared final int w = 'a';



your code will not compile buddy ... it will still show the same error ... required java.lang.integer

it is because we need to supply
1. constants to a switch statement
2. case of the same data type used in the switch clause ...
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
two things to remember here:
1. case argument takes only constant arguments.
2. switch argument cannot be downcast.

 
Rajeev Rnair
Ranch Hand
Posts: 310
1
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


your code will not compile buddy ... it will still show the same error ... required java.lang.integer

it is because we need to supply
1. constants to a switch statement
2. case of the same data type used in the switch clause ...



You are very correct! The funny thing is the above code compiled in an Eclipse IDE When I used command line it gives the error "required java.lang.Integer"
If we take out the case 'b' it compiles. Please see code below: It compiles both in Eclipse and command line!

 
Rajeev Rnair
Ranch Hand
Posts: 310
1
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ankit, Lalit and Punit for the explanation, it makes sense!!
 
Lalit Mehra
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Rajeev ... your welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic