• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Q from examulator.com

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whether 'char' can be considered as integer type?
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Alfred & Barry.
Srini
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for clarifying Alfred..!!!
I got it confused with something else.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic