Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

switch case

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Why case �25� is showing error but case �1� and case �4� are not showing error since all are inside the char range? Help please
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A character constant can only contain one character.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To expand on what Keith said...

Assigned to a char variable, an int literal (without quotes) represents a char value. For example, a decimal value of 51 represents the char '3'.

char three = 51;

But an int inside single quotes is interpreted as a char.

char three = '3';

Similarly, '5' is a char, and '1' is a char. But '51' is not a char.

(And of course, an int inside double quotes is interpreted as a String.)
 
kwan Jang
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just one character,such char a = 'b'; char n='2'
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic