About Question enthuware.ocajp.i.v7.2.1221 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

About Question enthuware.ocajp.i.v7.2.1221 :

Post by ETS User »

Hi,
I thoroughly understand why the result it's byte b = 320; the int value 320 cannot fit the byte in question.

I would like to point out that even if it's done a typecast as "byte b = (byte)320;" The resulting value from the typecast expression will not be the maximum value of a byte which is 127, but it the least significant bits of int 320 will be cut out, hence the result won't be so easily predictable but it will be 64.

Does anybody have a good web reference explaining the bit cutting game?

I am pointing this out hoping might be useful for exam takers to smash the OCA.

The_Nick.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1221 :

Post by admin »

Although not required for OCAJP, but the responses posted here are very useful for understanding how it works.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1221 :

Post by admin »

As an exercise, try this program with different values for a and b and see the bit patterns. This will tell you how the truncation works while truncating an int into a byte.

Code: Select all

public class BitsTest {
    public static void main(String[] args) {
        int a = 128;
        System.out.println(Integer.toBinaryString(a)+" val = "+a);
        byte b = (byte) 128;
        System.out.println(Integer.toBinaryString(b)+" val = "+b);
    }
    
}
It prints:

Code: Select all

                        10000000  128
11111111111111111111111110000000  -128
You will notice that the bit patterns at the last 8 bits of int as well as byte are the same. However, the position of sign bit in an int is and a byte is different (32nd bit from right for int and 8th bit from right for byte).

So observe that in case of an int 128, the sign bit is 0 (the blanks are zeros) but in case of byte, the sign bit is 1. So both the numbers are interpreted differently and that is why you see -128 printed for byte.

Try it with different numbers.

The_Nick

Re: About Question enthuware.ocajp.i.v7.2.1221 :

Post by The_Nick »

Thanks for the precious exercise.
I will do that in order to better understand the topic and therefore be better prepared to the OCA.

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1221 :

Post by gparLondon »

How is option e legal then? In the explanation it clearly says that option e will not compile!!!!

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1221 :

Post by admin »

The question asks which of the options are "illegal". e is legal and is therefore marked incorrect.
The explanation explains why this options is legal and it also shows another similar expression, which is illegal.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 7 guests