File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can't understand such an output.. 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 "Can Watch "Can New topic

Can't understand such an output..

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435

Options :
1)It generates a compile error at line 4
2)It generates a compile error at line 5
3)If the cast on line 5 were miited, then line 5 would not compile
4)The code compiles and prints "b=0"
5)The code compiles and prints"b= -2"
6)The code compiles and prints "b= -3"
7)The code compiles and prints "TROUBLE"
Ans :3) and 6)
I cannot understand from where did this "-3" come?
Please explain?
Rob Ross

Joined: Jan 07, 2002
Posts: 2205
All numbers in java are signed, ie, they can be + or -.
The lines
short s = 0x00FD;
byte b = (byte)s;
cause the value 0xFD to be stored in the variable b.
0xFD is hexidecimal for -3 using signed numbers.
just remember that OxFF is -1, and count backwards from there...
FF = -1
FE = -2
FD = -3
FC = -4...
80 = -128
7F = 127
7E = 126...

SCJP 1.4
I agree. Here's the link:
subject: Can't understand such an output..
It's not a secret anymore!