Hey ppl, In RHE talking about Unary operators the authors say(their words): "For unary operators, two rules apply, depending on the type of the single operand: * If the operand is a byte, a short, or a char, it is converted to an int (unless the operator is ++ or --, in which case no conversion happens). * else there is no conversion." Now, if I do this: <code> char c='j'; c++; System.out.println(c); </code> it does display char 'k'. So 1) what exactly does this "there sill be no conversion meanins?" 2) I guess char c was converted to int, applied the ++ operator and again converted to character. Am I right? Thanks,
Originally posted by Anup Engineer: * If the operand is a byte, a short, or a char, it is converted to an int (unless the operator is ++ or --, in which case no conversion happens).
Didn't you write this yourself?
And you don't believe your own writing? There is no conversion necessary here: char c = 'j'; c++; c is of type char, but don't forget that char is an integer type (Not int, integer, as opposed to floating point.) It's a 16 bit quantity. Adding one to it is something easily done without the need to convert it. This is one of those special cases you need to remember. The following, however, causes the result to be converted to an int: c +=1; However, it's also implicitly narrowed back to a char for you! But this will result in a compiler error: c = c + 1; //must explicitly cast result type back to char