• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

int to byte conversion/casting  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Answer Choices:
- Compilation Error
- Compile and print 100
- Throw a runtime exception
My answer to this was (Compilation error), as Im trying to assign a 32 bit int to an 8 bit byte, hence loss of precision. However the compiler doesnt complain and instead compiles fine and prints out 100. If i remove the final from int i = 100; declaration and the rest of the code remains the same i do get a compilation error.
Comments? Thoughts?
[ July 31, 2003: Message edited by: SMZ ]
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code compiles fine and output is 100
Here final int i is a compile time constant. You don't have any casting problem. If you remove final from 'final int i' you would get possible loss of precision error. The answer is 'Compile and print 100'.
 
S Zaidi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah now it makes sense....
Thanx so much for your help Venu
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the same topic:

will compile and print 30
although here the compiler will balk at line 3 because of possible loss of precision:
 
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!