Two Laptop Bag
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes int to byte conversion/casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "int to byte conversion/casting" Watch "int to byte conversion/casting" New topic

int to byte conversion/casting

S Zaidi

Joined: Jul 31, 2003
Posts: 9

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 ]
venu gopal
Ranch Hand

Joined: Nov 21, 2000
Posts: 40
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

Joined: Jul 31, 2003
Posts: 9
Yeah now it makes sense....
Thanx so much for your help Venu
cyril vidal
Ranch Hand

Joined: Jul 02, 2003
Posts: 247
On the same topic:

will compile and print 30
although here the compiler will balk at line 3 because of possible loss of precision:

SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287
I agree. Here's the link:
subject: int to byte conversion/casting
It's not a secret anymore!