• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java type promotion query  RSS feed

 
Denis Broderick
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I've a small question relating to type promotion I can't find an answer for on the web.

basically in your code if you have

byte b = 0;
b = b + 1;

the compiler will complain about the result being an int which cannot be assigned to a byte. That I understand, as b on the right hand side of the expression is promoted to an int and the result of the addition is an int.

However the following does compile

byte b = 0;
b++;
Does the post increment not carry out the post increment as "give me the value of b and then add 1 to b" where I would have expected 'add 1 to b' to do the same integer promotion as the previous example ?

The compiler will also allow the following

byte b = 0;
b += 1;
Again , why is no type promotion happening here ?

Any explanations of the above is greatly appreciated :-)
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Denis: Welcome to the Ranch!

@Ninad: the OP has posted a specific, well thought out and tested question, but your link is to a very long, complex, and general page. It you know the answer to the OP's question, I think he deserves it.

I think I know part of the answer: byte b; b = b + 1; fails because the compiler is adding an int (1) to a byte and it doesn't know if you'll lose information doing so. With b++, you are saying, Increment byte, so no conversion takes place.

Still a mystery is why b += 1; works. It may be that the compiler optimizes this to b++;
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Still a mystery is why b += 1; works. It may be that the compiler optimizes this to b++;

What's even more interesting is thatalso compiles. The result is 65.
I expect the answer lies in the JLS but it is not an easy read.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote: . . .
Still a mystery is why b += 1; works. It may be that the compiler optimizes this to b++;
No. It is more complicated. If you go through the Java Language Specification, you find it is a widening and narrowing conversion. If you look in a book like Java Puzzlers by Bloch and Gafter, you find they recommend you avoid += for any datatype less than 32 bits wide, because you can get confusing results.
 
Denis Broderick
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks folks for the replies & thanks Knute for the ranch welcome , it's certainly a fantastic resource for getting an explanation for some of the oddities in Java.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was mistaken about widening and narrowing, but it is rather like that. The Java Language Specification (=JLS) describes it, in the usual impenetrable style you find in the JLS. You get expansion of type, then conversion back to the original type which may be widening followed by narrowing. The narrowing conversion later on may lose information.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!