17

With Java, I'm reading a book to re-cover the basics I forgot in college, and they're showing me a swtich statement like so:

void helpon(int what) {
    switch(what) {
        case '1': break;
        case '2': break;
    }
}

I omitted case code because irrelevant.

However, it seemed odd to me to use an int and still wrap the case statements in single quotes, so I went to the oracle docs and found an example that was the same as the example above, but without the quotes.

Do quotes matter for a switch statement that uses integers as a case? Why would '1' work, if what is an int and '1' is a char?

Sterling Archer
  • 22,070
  • 18
  • 81
  • 118
  • 2
    It's actually a `char`. – August Aug 08 '14 at 15:53
  • Ah, of course sorry. I'm so used to javascript these variable types are unfamiliar! – Sterling Archer Aug 08 '14 at 15:53
  • `'1'` and `1` are different. `1` is an integer, while `'1'` is a `char`, which can be typecasted to an `int`. but typecasting it won't produce the value you think of: the character `'1'` is actually the integer `49` (the ascii code for the character `1`) – BackSlash Aug 08 '14 at 15:55
  • @BackSlash with that logic, how can the single quoted switch case run, if `what` == 1 and the case is apparently, 49? – Sterling Archer Aug 08 '14 at 15:57
  • @SterlingArcher Simple: It can't run :) – BackSlash Aug 08 '14 at 15:57
  • @BackSlash oh, so my book is... wrong? lol – Sterling Archer Aug 08 '14 at 15:57
  • @SterlingArcher Yes. Unless `what` is a `char` typecasted to `int`. Something like `helpon((int)'1')` – BackSlash Aug 08 '14 at 15:58
  • @SterlingArcher May I ask, which book is it? – BackSlash Aug 08 '14 at 16:02
  • @BackSlash [this one](http://www.amazon.com/Java-Beginners-Guide-5th-Edition/dp/0071606327) – Sterling Archer Aug 08 '14 at 16:04
  • @BackSlash Of course it will "run". What it does (or does not) is another matter. - The book contains an exercise where the variable `choice` after `switch(` is a char, casted from a `read()` method call. – laune Aug 08 '14 at 16:19
  • @laune - yes, it will run. I understood it like "so if 'what' is 1 and the case is 49, it will never pass?"... That's why I answered "yes" – BackSlash Aug 08 '14 at 16:26
  • @BackSlash oh, I didn't mean that. If `what` is 1 and the case is `1`, which everybody said converts to the int 49, will it still run? Or will the quoted 1 be converted and match? – Sterling Archer Aug 08 '14 at 16:28
  • @BackSlash I think the confusion has reached a maximum. Is your last comment based on the code snippet based in the question? -- (1) `what` is an int. (2) If it has the value 49 (50) the first (second) case alternative is executed. (3) All other values pass through the statement without causing anything. – laune Aug 08 '14 at 16:48
  • Does the book also say where `what` is coming from? Does it happen to be read from the keyboard? – CompuChip Aug 08 '14 at 21:38
  • An **int** doesn't want quotes. – Phantômaxx Aug 27 '14 at 10:24

4 Answers4

15

Single quoted characters are literal chars and char values fall in int values. A char can be represented as an int from its UTF-16 value, for example:

char c = '1';
int i = c;
System.out.println(i);

Output:

49

More info:


Do not get confused between ASCII characters (single byte, value from 0 to 127) and UTF-16 (2 bytes per code point, value from 0 to 65535). More info: Unicode, UTF, ASCII, ANSI format differences

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • 1
    I'm afraid I don't know what that means -- could you explain a bit more or link to the docs for an explanation? – Sterling Archer Aug 08 '14 at 15:54
  • 1
    If the JVM determines that in that context the char should be converted as an int, it is replaced by its ASCII code. For example `int i = '0' - 48;` is equivalent to `int i = 0` since characters '0'-'9' correspond to integers 48-57. – Dici Aug 08 '14 at 15:55
  • It kind of makes sense. I'm reading through the documentation now and it's getting clearer to me. Thank you! – Sterling Archer Aug 08 '14 at 15:59
  • @Dici Not "determined by the JVM", it's the Java compiler. – laune Aug 08 '14 at 16:08
  • 1
    This is nitpicking, but there's no "UTF-16 value". UTF-16 encodes Unicode code points to bytes. Unicode contains more than 65,535 characters, so UTF-16 is not a fixed width encoding. Code points above U+FFFF are represented using surrogate pairs. – ntoskrnl Aug 08 '14 at 19:18
3

I just want to add some depth. Yes '1' is the char of 1 which is stored as an int value and that is why you can use an int in a char declaration (single quotes is the other way to do it). However, char is stored as a range with a maximum of 65,535 (Java is utf-16 I believe so 2^16). You cannot have a negative int value in char without casting and casting will yield an odd character. A value greater than the range is a problem.

A switch will take the following int,char,short,byte (anything that fits in an int) and with SE7 a String. Basically, the primitives and a String object are acceptable. You could turn the primitive into an object such as Integer, Character,Short;Byte.

Technically with char you can use either int within range or a char for the case as long as you are checking the right thing.

With an int anything that automatically fits into an int should be used. Casting in the switch parameter causes a loss of precision.

For good measure, don't forget to break or you will fall through. Also, the case statements must be constants determinable at compile time, either arithmetic of known and acceptable primitives, a known primitive using the final modifier, or a directly coded constant.

Andrew Scott Evans
  • 1,003
  • 12
  • 26
1

The chars '1' & '2' in this care are being interpreted as their UTF-16 value. So '1' is really being evaluated as 49, '2' as 50, and so on.

I need more rep to post pictures but here's an ASCII code chart. http://asciiset.com/asciitable.gif.

Weston
  • 1,291
  • 3
  • 12
  • 25
0

The char will be converted to UTF-16, and then it will be compared. Also after Java7 you can also write String in switch cases.

Vishrant
  • 15,456
  • 11
  • 71
  • 120