File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ++ promotion for byte,short char? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "++ promotion for byte,short char?" Watch "++ promotion for byte,short char?" New topic
Author

++ promotion for byte,short char?

Mario Levesque
Ranch Hand

Joined: Nov 01, 2000
Posts: 51
Hi,
In RHE p 107 the following rules are given:
unary operators:
If the operand is a byte, short or char, it is converted to an int,
If the operand is an int or larger it is not converted.
However the following works.

If the ++ operator promotes to an int, why can I assign an int into s which is a short without any problems?
Thanks for your help.


<a href="http://www.ajmasters.com" target="_blank" rel="nofollow">http://www.ajmasters.com</a> Real Estate, Tampa Florida
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
From JLS 15.15.1 Prefix Increment Operator ++


At run time, if evaluation of the operand expression completes abruptly, then the prefix increment expression completes abruptly for the same reason and no incrementation occurs. Otherwise, the value 1 is added to the value of the variable and the sum is stored back into the variable. Before the addition, binary numeric promotion (�5.6.2) is performed on the value 1 and the value of the variable. If necessary, the sum is narrowed by a narrowing primitive conversion (�5.1.3) to the type of the variable before it is stored. The value of the prefix increment expression is the value of the variable after the new value is stored.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Mario Levesque
Ranch Hand

Joined: Nov 01, 2000
Posts: 51
Thanks Valentin.
If I get this right, in this case, the short variable and the number 1 are promoted to int, added together, then the result is casted back to a short.
Mario
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
yes but be aware that if you cross the upper bound then the value is wrapped.
in clear, the following code yields -32768 instead of 32768 because 32767 is the upper bound for a short.
Anup Engineer
Ranch Hand

Joined: Mar 04, 2002
Posts: 48
Hello Mario, I had asked the same doubt couple of weeks back:
think abt this:
char c='i';
c++;
System.out.println(c);
char c was not required to be cast to int, perform the ++, and cast back to char. THE POINT IS, for byte,short and char, you dont need to.
also about the other point being discussed:
short s=3; works
but
int i=3;
short s=i;
gives a compile time error. (possible loss of precision). This is because only in two cases the compiler performs implicit downcast. First, in an assignment like the one above(only works for int and lower order) another is the "op=" operator, where you can say
byte b=0;
b+=1;
here b is promoted to an int, performed the +=1 and cast back to byte.
Please correct me if I am wrong!!
HTH,


Anup Engineer
Mario Levesque
Ranch Hand

Joined: Nov 01, 2000
Posts: 51
Thanks guys, it all helps.
I guess the example with a char really makes it clear why a promotion to int is necessary. With byte and short it is not very obvious. After all it is only adding 1, why would it needs a 32bit overhead to do that... but with char it makes some sense why it needs to promote to a common ground.
The example with overflow is something else also.
Funny how some much info is involved with those little ++

Thanks again,
Mario
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ++ promotion for byte,short char?