• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q from examulator.com

 
Srinivasan Mani
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The char type is unsigned. It has a range
0 to (2^16)-1 ( here ^ represents "to the power of").
-Barry
 
Srinivasan Mani
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether 'char' can be considered as integer type?
 
Alfred Kemety
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Srinivasan Mani
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Alfred & Barry.
Srini
 
shweta mathur
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alfred,
Can u explin the following more ..
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 !
 
Alfred Kemety
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
shweta mathur
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for clarifying Alfred..!!!
I got it confused with something else.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic