jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Arithmetic promotion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arithmetic promotion" Watch "Arithmetic promotion" New topic

Arithmetic promotion

Bob Young
Ranch Hand

Joined: Dec 23, 2000
Posts: 65
class Testing {
public static void main(String[] args) {
short s;
for(short i = 0; i<10; i++) {
s = 0;
s = i;
In this example the short i should be promoted to an int when it is incremented in the for loop(i++). But if this were to happen then i could not be assigned to short s because it would be a a narrowing conversion. This code works fine, but why?
Michael Hildner
Ranch Hand

Joined: Oct 13, 2000
Posts: 297
Interesting, apparently i is never promoted to an int. When I changed i++ to i = i + 1, the compiler complained.
Joseph Russell
Ranch Hand

Joined: Jan 08, 2001
Posts: 290
The ++ operator doesn't cast the data type into an int instead it treats it as what it is. However when you do a = a + 1; than it gets converted into an int and in order for it to work a would need to be cast into a short.
Steve Fahlbusch

Joined: Sep 18, 2000
Posts: 557

For an autoincrement ++ or autodecrement -- a short does not need to be promoted (in fact it should not). This stems from the old c operators that used ++ so one could increment using the hardware instructions rather than doing an add immediate 1.
Bob Young
Ranch Hand

Joined: Dec 23, 2000
Posts: 65
I posted this question after reading the arithmetic promotion section in Java 2 Cert. Study Guide, RHE. The book states "for unary operators, two rules apply, depending on the type of the single operand:
- If the operand is a byte, a short, or a char, it is converted to an int.
- Else if the operand is of any other type, it is not converted."
Furthermore, they have this as an example:
short s = 9;
int i = 10;
float f = 11.1f;
double d = 12.2;
if(++s * i >= f / d)
"With the rules in mind, it is possible to determine what really happens in the code example given at the beginning of this section:
1. The short s is promoted to an int and then incremented.
2. The result of step 1 (an int) is multiplied by the int i. Since both operands are of the same type, and that type is not narrower than an int, no conversion is necessary. The results of the multiplication is an int.
3. ...
4. ..."
If you change my code slightly to make s a byte, you will get a compile error:
class Testing {
public static void main(String[] args) {
//short s;

for(short i = 0; i<10; i++) {
byte s = 0;
s = i;
compiler error - Incompatible type for =. Explicit cast needed to convert short to byte.
s = i;
The compiler is saying that i has not been converted to an int! Here is another example:
class Testing {
public static void main(String[] args) {
short i, s = 0;
i = s;
System.out.println("s = " + s);
System.out.println("i = " + i);
output is:
s = 1
i = 1
Did RHE get this wrong? I'm just scratching my head.
Peter Tran

Joined: Jan 02, 2001
Posts: 783
I don't see anything wrong with your example.
The first won't compile, because a byte is 1 byte while a short is 2 bytes. Going from a larger datatype (short) to a smaller datatype (byte) is a narrowing conversion which always require a cast.
To get it working correctly, you'll need byte b = (byte)s.
In the second example, both i and s are short so assigning one to the other isn't a problem.
Bob Young
Ranch Hand

Joined: Dec 23, 2000
Posts: 65
Peter, the last post was an attempt to clarify why I am confused on the issue of promotion when using the ++/-- operators. In the posting I quoted the text from RHE. In it, it states that a short will be promoted to an int when using the ++/-- operators. The examples I gave support just the opposite of the RHE text.
In the first example of today's post, the compiler is stating that i (the i++ in the for loop) has not been promoted to an int and in fact is still a short (via the compiler error). In the second example, s has not been promoted to an int as can be seen in the output. If s had been promoted to an int I would have gotten an error message.
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Depends on which version of the RHE you have. In the errata for the old book it says:

Page 107
Corrected in the third printing: The first bullet should read, "If the operand is a byte, a short, or a char, it is converted to an int (unless the operator is ++ or --, in which case no conversion happens)."

This is exactly what you experienced. In your second post when you showed:
if(++s * i >= f / d)
it is the "* i" that causes the promoting to an int, NOT the ++s.
The errata for the old book is at: http://scooter.sybex.com/erratatracking.nsf/weberrataform?OpenForm&ISBN=2700

The errata for the new book is at: http://scooter.sybex.com/erratatracking.nsf/weberrataform?OpenForm&ISBN=2825
[This message has been edited by Cindy Glass (edited February 14, 2001).]

"JavaRanch, where the deer and the Certified play" - David O'Meara
Bob Young
Ranch Hand

Joined: Dec 23, 2000
Posts: 65
Thank you so much for the leg work on this. I now know why I was able to get the book so cheaply. It is the original printing. I had some strange thoughts going through my head on how this could be happening!

Joined: Feb 19, 2001
Posts: 6
in this case i think you are doing imlpecitly cast
short t=10;
t=t/2;//here cast require
t/=2;//implecitly cast

if you clear all
then i want a question

int i;
after exec. you found i is 0
why that
I agree. Here's the link: http://aspose.com/file-tools
subject: Arithmetic promotion
Similar Threads
how to assign some numeric values like 1 ... to a short variable ?
equals or identity
Please explain....!!!!
Regarding main()
"Short " - Why does this happen?