Binary Numeric Promotion

From the JLS - When an operator applies binary numeric promotion to a pair of operands, and these are both characters, both operands are converted to type int.

So given

byte b ='b' +'a';

I see a compiler error as I would expect. The operands would be converted to type int and the result could not be assigned to a byte without a cast.

However, the following all compile and run successfully..

byte b ='b' -'a';

char c ='b' -'a';

char ch ='b' +'a';

But how? Aren't the characters 'b' and 'a' converted to type int and doesn't this preclude direct assignment to byte or char without an explict cast? What is the difference?

[1049 byte] By [nantucketa] at [2007-10-2 12:57:56]
# 1

I think in all cases, the result is a compile-time constant that the compile knows can fit into a byte, so it's cool.

int ii = 'b' - 'a';

char c1 = ii; // I expect this to fail

byte b1 = 127; // I expect this to succeed

byte b2 = 128; // I expect this to fail

jverda at 2007-7-13 10:15:41 > top of Java-index,Java Essentials,New To Java...
# 2

[url http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.2]JLS 5.2 Assignment Conversion[/url]: A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.

jverda at 2007-7-13 10:15:41 > top of Java-index,Java Essentials,New To Java...
# 3

int i1 = 'b' - 'a';

char c1 = i1; // I expect this to fail

final int i2 = 'b' - 'a';

char c2 = i2; // I expect this to succeed

Note that I haven't tested any of the code I posted. My expectations are just semi-educated guesses.

jverda at 2007-7-13 10:15:41 > top of Java-index,Java Essentials,New To Java...
# 4
> I think in all cases, the result is a compile-time> constant that the compile knows can fit into a byte,...or into a char in the case of your ch.
jverda at 2007-7-13 10:15:41 > top of Java-index,Java Essentials,New To Java...
# 5

Thanks for your posts jverd. You've helped me see that I was overlooking the obvious. That is, the actual value of 'b' + 'a' is outside the upper range of byte which is 128. I was looking just at the 'rules' and not attending to the resultant value.And of course the range for char is much wider.

Thanks for your responses.

nantucketa at 2007-7-13 10:15:41 > top of Java-index,Java Essentials,New To Java...
# 6
You're welcome. I'm glad I was able to help clarify this.
jverda at 2007-7-13 10:15:41 > top of Java-index,Java Essentials,New To Java...
# 7
> Thanks for your posts jverd. You've helped me see> that I was overlooking the obvious. That is, the> actual value of 'b' + 'a' is outside the upper range> of byte which is 128. 127, actually. :-)
jverda at 2007-7-13 10:15:41 > top of Java-index,Java Essentials,New To Java...
# 8

> > Thanks for your posts jverd. You've helped me see

> > that I was overlooking the obvious. That is, the

> > actual value of 'b' + 'a' is outside the upper

> range

> > of byte which is 128.

>

>

> 127, actually. :-)

LOL. Yes indeed.

nantucketa at 2007-7-13 10:15:41 > top of Java-index,Java Essentials,New To Java...