File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Conversion" Watch "Conversion" New topic
Author

Conversion

NDP Prasad
Ranch Hand

Joined: Apr 13, 2006
Posts: 177
The below code is from Dan Chisholm

class Magenta {
static byte a = (byte)127, b = (byte)128, c = (byte)255, d = (byte)256;
public static void main(String args[]) {
System.out.print(a + " " + b + " " + c + " " + d);
}}

What is the result of attempting to compile and run the program?

a. Prints: 127 128 255 256
b. Prints: 127 128 255 0
c. Prints: 127 -1 -127 0
d. Prints: 127 -128 -1 0
e. Run-time error
f. Compile-time error
g. None of the above


I want to know the output of the program and Explanation of how it comes?


SCJP,SCWCD<br />
Priya Viswam
Ranch Hand

Joined: Dec 28, 2006
Posts: 81
Output is d : Prints 127 -128 -1 0

since 127 can fit into a byte 'a' will print 127

128 in binary is 1000 0000. Here the first bit is the sign bit. First bit 1
indicates it is a negative number. Inorder to get the actual value we have
to take 2's compliment.
For getting the Twos compliment we have to find the 1's complement and add 1 to it.

1000 0000 -> 0111 1111 (1's compliment) -> 1000 0000(2's complement) -> 128
So b will print -128.

Similar way c will print -1.
255 in binary is -> 0 1111 1111. After casting to byte it will become 1111 1111. Here also the sign bit is 1 and so we have to take 2's complement.
1111 1111 -> 0000 0000 -> 0000 0001
So c will print -1

256 in binary is -> 1 0000 0000. After casting to byte it will become 0000 0000. Sign bit is zero. So no need to take the 2's complement and the value is zero.


SCJP 1.5<br />SCWCD 1.4
NDP Prasad
Ranch Hand

Joined: Apr 13, 2006
Posts: 177
Thank you Priya. I got it.
Anu Rastogi
Greenhorn

Joined: Jan 08, 2007
Posts: 3
I am also in the process of preparing for JCP. So please correct me if my understanding is incorrect

A byte is represented by 8 bits of which the leftmost bit is the sign bit. This is 1 for negative numbers and 0 for positive numbers. The range for a byte is -128(1000 0000) to 127(0111 1111).

When byte a =(byte)127 is evaluated, 127 is in the range for a byte, so the output is 127.

For byte b=(byte)128, this is represented in binary as 1000 0000 which is equivalent to -128.

For byte c=(byte)255, this is represented as 1111 1111. This is a negative number. A negative of a number is found by taking 2s complement.(taking 1's complement and adding 1). So if we reverse this process on this number(subtracting 1 and taking 1s complement), we get -1.

For byte d=(byte)256, this is represented in binary as 1 0000 0000. Since this has 9 bits and a byte is represented with 8 bits, the 9th bit is discarded and we get 0 as the result.
Pratap koritala
Ranch Hand

Joined: Sep 27, 2006
Posts: 252
We can also find the result with arithmetic division rather than dealing with bits.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Conversion