• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Chapter 2 CASE statement topic question (pg 73 )  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I was working on review question 20 and came across this scenario.
According to the book, " the values in each case statement must be compile-time constant values of the same data type as the switch value" .
Though the switch value type is 'char' and the statement  'case 1' below has value type of int, the program is compiling without any errors. I was expecting it to give a compile error when it encounters case 1.  
What am i missing?  TIA !

 
Rancher
Posts: 810
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In your code and my code, the 1 is autoboxed as a char corresponding to the nonprintable char with numeric value of 1, not '1'. When it is cast back, you can see the value has not changed.
 
Marshal
Posts: 64088
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't try coloured text or bod print inside code tags; as you have seen, it doesn't work. I have deleted the tags.
 
Run away! Run away! Here, take this tiny ad with you:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!