File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confused Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "confused" Watch "confused" New topic


sonali rao
Ranch Hand

Joined: Nov 03, 2003
Posts: 64
got some doubts. Please help me out .
1.What is the result that will be printed out here ?
int i = - 128;
System.out.println((byte)i << 1);
answers 1: -256
2: 256 .
Correct answer is -256. but i thought it would be 256.
2.Will this test evaluate to true?
Object a = "hello";
String b = "hello";
if(a == b)
System.out.println("not equal");

Answer given is "Yes". but when i compile i get the following 2 errors.
error: incompatible types
found: java.lang.String
required: Object
Object a="hello";
error: incomparable types: Object and java.lang.String
Thanks in advance.
Brahim Bakayoko
Ranch Hand

Joined: Aug 29, 2003
Posts: 155
1. the sign is preserved given the shift (<<
2. the answer should be "not equal"
This would be true:
String s = "hello";
if(s == "hello")... only because of compiler optimization...
This would be false:
String s = "hello";
if(s == new String("hello"))...
This would be true also:
String s = "hello";
String s2 = "hello";
if(s == s2)... only because of compiler optimization...

SCJP, SCWCD, SCBCD, IBM CSD WebSphere v5, <br />A+, MCP 2000 and 2000 server, CST, and few incompleted certification tracks.<br /> <br />Ivory Coast<br /> <br />Analyze your web Request/Response @ <a href="" target="_blank" rel="nofollow"></a> down for a while...
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
"System.out.println((byte)i << 1);"
The cast operator has a bigger precedence than <<. Thus "i" is interpreted as a byte for the next operation, however "<<" converts "i" to int, thus the cast to byte has no effect.
"System.out.println( (byte)(i << 1) );" prints 0 because the 8 least significant bits of -256 are 0.
[ November 04, 2003: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
S. Sumukh
Ranch Hand

Joined: Aug 27, 2003
Posts: 31
Hey Sonali,
Plz visit
JLS String Literals
It shud answer ur 1st question.
and I was able to compile the code

and o/p as expected is equl as 'a' and 'b' will refer
to same "hello".
Which java version u r using ?? I used 1.4.1..
I agree. Here's the link:
subject: confused
It's not a secret anymore!