• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

++ promotion for byte,short char?

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Mario Levesque
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Mario Levesque
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic