2

What is the difference between using the short forms and the long forms in Java? Look at the following code:

char myChar = 'p';
myChar += 2;
myChar++;
myChar = myChar + 2;        
System.out.println(myChar);

Line 2 and 3 work like expected. Line 4 gives the error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
        Type mismatch: cannot convert from int to char

I thought line 2 and 4 are the same. But the seem to be not the same?

timmornYE
  • 708
  • 2
  • 8
  • 22
  • 1
    In case 1 and 2. there is an *implicit conversion* from int to char. If you look at the byte-code you will see `i2c` instruction. Case 3, you will have to explicitly convert it to char. – TheLostMind Nov 10 '15 at 06:12
  • 1
    [Difference between a += 10 and a = a + 10 in java?](http://stackoverflow.com/q/2081932/995714), [Java += operator](http://stackoverflow.com/q/8710619/995714) – phuclv Nov 10 '15 at 06:45

4 Answers4

3

In the unary forms it's implicit that the operand's type is unchanged.

In the binary form, the addition of the integer 2 causes the type of the whole expression myChar + 2 to be promoted to int, causing the assignment back to the char myChar to fail.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • So... `myChar = myChar + (char)2`? – David Ehrmann Nov 10 '15 at 06:08
  • By the quote from a now-deleted answer by @thegauravmahawar, `myChar += 2` is not equivalent to `myChar = myChar + 2`, but to `myChar = (char) (myChar + (char) 2))` ([source](http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.26.2)). – Amadan Nov 10 '15 at 06:10
  • also - `myChar = myChar + (char)2` doesn't work - you get a "possible loss of precision" error. – Alnitak Nov 10 '15 at 06:12
1

For

myChar += 2;

From JLS 15.26.2:

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

So, it is equivalent to:

myChar = (char) (myChar + 2);

As for

myChar = myChar + 2;

myChar is promoted to int and added to 2. Now you are assigning this value which is an int to char which results in the error.

thegauravmahawar
  • 2,802
  • 3
  • 14
  • 23
0

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

The statement myChar+ = 2; will be implictly converted into myChar =(char) (myChar + 2);

Balayesu Chilakalapudi
  • 1,386
  • 3
  • 19
  • 43
  • no, it won't - it'll be implicitly converted to `(char)(myChar + 2)` - not quite the same thing – Alnitak Nov 10 '15 at 06:10
0

This is due to automatic promotion.

int+char will result to int
int+double will result to double

Now if you try to

int +char ---> store to char
int +double ---> store to int

this will cause an error.


but if you try

char c='a';
c++;

it is equal to

char c = 'a';
c = (char)(c+1);

instead if you use

char c = 'a';
c = c+2;//error

because it is

char + int ---> store to char(explained above)//error

hence for writing c = c+2; use c = (char)(c+2); now this works as follows.....

c+2; is basically (char + int) so the result is int(& not char)

when you do (char)(c+2); the int value is converted to char


Note

The limit of char in java is from 0 to 65535. So if the result of

(char)(char + int)---> store to char

overflows the value then you get unexpected output.

for example

char c  = (char)65535;//valid
c++; will result as c = 0 (not 65536) due to the limit of char.
Doc
  • 10,831
  • 3
  • 39
  • 63