hi.. This question is from the Exam Cram tests.Chapter 7. The question says "Which casts do not lose information?" 1) long lx = 0x05544332211L; int ix = (int) lx; 2) short sx = (short) 0x7654; char cx = (char) sx; (3) long lx =0x0fffL ; short sx = (short) lx;
4) int ix = (int) 37; byte bx = (byte) ix; But here all the castings r "narrowing conversions" (Casting is done when narrowing conversion is reqd.") So this is ok...But when narrowing conversions r done information is lost[as given in RHE].. SO then how come answers (2) and (4) correct?? I feel all the the options lose information.. And so automatically it conforms that all castings will lose information.. Please correct me if i am wrong. My second question is.. We cannot convert a byte to char or char to short..(narowing conversions) So then can we do it by casting???(explicit conversion)
lets look at your first question, which is, why is answer 2 and 4 correct. now for answer 4(because it's easier to explain and more imaginable) int ix = (int) 37; byte bx = (byte) ix; The no 37 can easily fit to an "int" because 37 needs only one byte while an "int" can store a number of 4 bytes(i.e. ix=37 which is 0000 0000 / 0000 0000 /0000 0000/ 0010 0101) now casting "ix" to a "byte" which has one byte and is "bx" will result in the literal of ix (i.e value of ix and NOT ix) to lose the leading three bytes only to have 1 byte left which must be the last one, this last one is (bx=(byte)ix i.e. bx = 0010 0101), so "bx" is still 37 and therefore you find no value loss now the same goes for question 2, lets look at it short sx = (short) 0x7654; char cx = (char) sx; now "short" has 2 bytes, so "sx" is (short)0x7654 ( sx = 0111 0110/ 0101 0100) now at 0x7654 must be casted to short because any integer literal (i.e integer value) is by default an integer !! now "char" has 2 bytes also, so "cx" like "sx" has two bytes !! in these case the "sx" bytes won't be cut off ans "cx" will take the same value and interrpert "sx" as a "char". usually you don't need casting when converting from char to int or long to float according to this figure char--------------\ ------------------int-->long-->float-->double byte-->short------/ (where \ and / are arrows pointing to int) but any conversion that doesn't apply to this figure like conversion from char to short MUST HAVE casting. I think the best thing for you to do is to read a book called "A Programmer's Guide To Java Certification(A Comprenensive Primer)" by Khalid and Rolf, I've already read this book and it's absolutely great, after that you can read your book and then take some mock exams on the net to get your certification, one place for mock exams on the net is http://www.javaprepare.com ... that of course is my opinion and you may ask for others...Go to amazon.com to find more info on the book !!
[This message has been edited by Jeffry Cray (edited October 15, 2000).]
You can tell which casts do not lose information by thinking about it this way - if you cast back to the original primitive value, would you get the same value? short sx = (short) 0x7654; char cx = (char) sx; short nx = cx ; nx == sx will be true Note that this question was about specific values! In general casting to a primitive that uses fewer bytes runs the danger of losing information, thats why the compiler insists on a specific cast. Bill