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?
Advertisements. A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.
There are four curly quote characters: the opening single quote ( ' ), the closing single quote ( ' ), the opening double quote ( “ ), and the closing double quote ( ” ). On Windows, hold down the alt key and type the four-digit character code on your numeric keypad (num lock must be activated).
Quotation Marks: Opening and Closing Quotation Marks. Once you have placed opening quotation marks at the beginning of an extract of exactly quoted words, do not add closing quotation marks until the speaker changes or you interrupt the quotation. "I'm not going to work today," he announced to his son.
Published works should use directional (or “smart”) quotation marks, sometimes called typographer's or “curly” quotation marks. These marks, which are available in any modern word processor, generally match the surrounding typeface.
Single quoted characters are literal char
s 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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With