Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arithmetic promotion

 
Bob Young
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Testing {
public static void main(String[] args) {
short s;
for(short i = 0; i<10; i++) {
s = 0;
s = i;
System.out.println(s);
}
}
}
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
Posts: 297
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting, apparently i is never promoted to an int. When I changed i++ to i = i + 1, the compiler complained.
 
Joseph Russell
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Joe
 
Steve Fahlbusch
Bartender
Posts: 605
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
System.out.println(">>>>>");
else
System.out.println("<<<<<");
"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;
System.out.println(s);
}
}
}
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;
s++;
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
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob,
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.
-Peter
 
Bob Young
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Bob Young
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
PUNNU GULATI
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
i=i++;
s.o.p(i);
after exec. you found i is 0
why that
punnugulati@yahoo.com
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic