I have a interesting question...

short a = 32767;

short c = 32767;// cc must be less than 32767

In this case, following statement make syntax error because "a + a " become int type.

c = a + a;

However, following statement does not make syntax error

c += a;

I guess the reason is "+= overloading method" has short type input parameters and return value.

I want to make sure what I knew. Does anyone know where I can get clear reference?

[451 byte] By [HyoHyo] at [2007-9-30 18:09:14]
# 1

For a "plus" expression (+) with two operands of primitve numeric type, the result is:

1. of a primitive numeric type

2. at least an int because of normal promotions

3. of a type that is as wide as the wider of the two operands.

4. of a value that is calculated by promoting the operans to the result type and performing the addition using those types.

filestream at 2007-7-6 18:40:44 > top of Java-index,Java Essentials,Java Programming...
# 2
> Does anyone know where I can get clear reference?[url= http://java.sun.com/docs/books/jls/second_edition/html/j.title.doc.html]The Java Language Specification[/url]
yawmark at 2007-7-6 18:40:44 > top of Java-index,Java Essentials,Java Programming...
# 3

There are a couple of things going on here.

First, the binary addition operator promotes its operands to either int, long, float, or double, depending on the highest-precision operand. The JVM does not provide arithmetic operations for bytes or shorts; they're promoted to ints.

Ref: http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#13510

Second, the compound assignment statement is actually translated like this:

E1 = (T)((E1) op (E2))

In other words, the result of the operation has an implied cast to the type of the result.

Ref: http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5304

kdgregory at 2007-7-6 18:40:44 > top of Java-index,Java Essentials,Java Programming...