• 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: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question from the new mock exam
http://www.geocities.com/danchisholm2000/june28/exam1.html
Q14

compiles and runs without any error.
i thought they should be an explicit cast to accomodate the long value in byte.

Now this results in the compiler error(loss of precision).
is b+=l any different from b=b+l?? If No, then why error in the later case?
Is arithmetic promotion taking place only in the former case?
please clear me on this.
thanks
zarina
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Zarina,
You are correct. If the simple assignment operator had been used as follows, then an explicit cast would be required.

The compound assignment operators do not require the explicit cast because the cast is implicit.

The rule is stated in section 15.26.2 of the
Java Language Specification.
The question was designed to catch those that know the first rule but not the second.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course the result of the operation would be wildly inaccurate, as precision is lost (long -> byte).
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,
If the result of the addition falls outside the
range of a byte, then the result would not be accurate. However, if the result falls within the range of a byte, then the result would be exactly correct. Even so, I agree that caution is required when a long value is added to a byte.
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
b=b+l; // Where can I put (long) ? Nowhere !
Because there are no places for you to put (long), the language designers have to mandate
that the compiler should do the casting for you.
And you are responsible for the lost of precision.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic