• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

switch statement

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why does this code produce "default" and not 1 ?
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because the unicode value of '1' will be taken to compare in the switch statement. This unicode value differs from 1, 2 and 3 so the default will be taken...

Greetz
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Geoffrey, I believe your explanation isn't quite correct.

The integer value of a char primitive is in fact the ASCII value of the character it represents. As it shows in the following code:



So even for char x = '1', the value x holds is the ASCII value of '1', which is 49.
[ May 25, 2004: Message edited by: Wendal Park ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wendal, the char does not have to be a ASCII character like 'a' or '1', it could be some value like \u0546 (whatever that is) which is way out of the ASCII range.


[ May 25, 2004: Message edited by: Barry Gaunt ]
 
Wendal Park
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh right, thanks for pointing out that.
I was just focusing on the single char (e.g. 'a') case which made me think of ASCII from the very beginning.
 
Always look on the bright side of life. At least this ad is really tiny:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!