Which of the following statements are true? 1)The following statement will produce a result of 1. System.out.println( -1 >>>2); 2)Performing an unsigned left shift (<<< on a negative number will always produce a negative number result 3)The following statement will produce a result of zero, System.out.println(1 >>1); 4)All the Java integral types are signed numbers Ans:3.Reason given for not choosing 4,is that 'char' is also an integer type which is unsigned.Is this correct?
char is an integer type, or more specific it can be promoted to int implicitly, but casted to byte and short explicitly as it is unsigned type so there might be a loss of precission with implicit casting. HTH
but casted to byte and short explicitly as it is unsigned type so there might be a loss of precission with implicit casting.
The following code runs fine without any explicit casting !
--Shweta<br />SCJP 1.4 <br />SCWCD
Joined: Aug 14, 2002
Shweta, in your code you're using literals which are compile time constants - meaning their values are known and fixed at compile time - and that's why the compiler knows there will be no loss of precision. But trying the following code you get this error: java:4: possible loss of precision found : short required: char char c = s; ^ 1 error
as the compiler doesn't know at compile time what value the short s will have. Same goes to byte, char, both ways char <--> byte/short HTH
Joined: Sep 23, 2002
Thanks for clarifying Alfred..!!! I got it confused with something else.