The moose likes Mock Exam Errata and the fly likes Bit operators Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Mock Exam Errata
Reply Bookmark "Bit operators" Watch "Bit operators" New topic
Author

Bit operators

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,have a look at the code:
Which of the following will compile without error?

1)
char c='1';
System.out.println(c>>1);
2)
Integer i=Integer("1");
System.out.println(i>>1);
3)
int i=1;
System.out.println(i<<<1);
4)
int i=1;
System.out.println(i<<1);
the Ans;1,4.i cant understand why it is 1 since 1 is treated
as Character.
Thanks!
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Originally posted by avn:
Hi,have a look at the code:
Which of the following will compile without error?

1)
char c='1';
System.out.println(c>>1);
2)
Integer i=Integer("1");
System.out.println(i>>1);
3)
int i=1;
System.out.println(i<<<1);
4)
int i=1;
System.out.println(i<<1);
the Ans;1,4.i cant understand why it is 1 since 1 is treated
as Character.
Thanks!

The char data type is a numeric data type. By assigning '1' to a char data type, you actually assign the unicode value that represents '1' on the platform you are on. Java allows and does implicate casting of char data type to an int data type when necessary. In the above, the char value of '1' is \u0031 or 49 so this gets auto promted to an int with the value of 49.

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
 
 
subject: Bit operators
 
Threads others viewed
Var args question...
Automatic Conversion / Cast
equals on array
BitShift
primitives/wrappers and var-args
developer file tools