• 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

Java type promotion query

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 :-)
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Refer Java Language Specification 8
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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++;
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
eat bricks! HA! And here's another one! And a tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic