File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "confused" Watch "confused" New topic
Author

confused

sonali rao
Ranch Hand

Joined: Nov 03, 2003
Posts: 64
hi
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("equal");
else
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
if(a==b)
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="http://webtools.servehttp.com" target="_blank" rel="nofollow">http://webtools.servehttp.com</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..
hth
sumukh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confused
 
Similar Threads
operator n assignment
Why this difference in conditional operator
Generics: mock exam question
Please Answer soon with explanation
Cannot understand the output...