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.
About Question enthuware.ocajp.i.v7.2.1221 :
Moderator: admin
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1221 :
Although not required for OCAJP, but the responses posted here are very useful for understanding how it works.
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1221 :
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.
It prints:
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.
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);
}
}
Code: Select all
10000000 128
11111111111111111111111110000000 -128
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.
Re: About Question enthuware.ocajp.i.v7.2.1221 :
Thanks for the precious exercise.
I will do that in order to better understand the topic and therefore be better prepared to the OCA.
I will do that in order to better understand the topic and therefore be better prepared to the OCA.
-
- Posts: 63
- Joined: Fri Oct 31, 2014 6:31 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1221 :
How is option e legal then? In the explanation it clearly says that option e will not compile!!!!
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1221 :
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.
The explanation explains why this options is legal and it also shows another similar expression, which is illegal.
Who is online
Users browsing this forum: Google [Bot] and 7 guests