• 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

Arithmetic promotion

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 297
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Or we might never have existed at all. Freaky. So we should cherish everything. Even this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic