RHE Second Chapter Test Yourself Q.No.5. Which of the following code fragment would compile and print "Equal" when run(Choose one or more) A. int x=100; Float y=100.0F; if(x==y){System.out.println("Equal");} B. int x=100; Integer y = new Integer(100);if(x==y){System.out.println("Equal");} C. Integer x = new Integer(100); Integer y = new Integer(100); if(x==y){System.out.println("Equal");} D. String x = new String("100"); String y = new String("100); if(x==y){System.out.println("Equal");} E. String x="100"; String y="100"; if(x==y){System.out.println("Equal");} The Answers are A & E. I agree with A but not with E. Since in E x and y are object ref. and will always give false when compared with == op. but here it is true since the java makes one copy of 100 and both x and y points to it. Please give a good reason of this. Thanks Martina Shahjee
Hello Martina, Both A and E are correct. E is correct because the strings are created from pool(not using new). Note: When Both strings are created without using new then == operator will compare only contents of two strings.Remember I said BOTH. Regards Gurpreet Sachdeva For Mock Exams, FAQ and some useful information about Bitshift operator, inner classes, garbage collection,etc please visit: http://www.go4java.20m.com [This message has been edited by Gurpreet Sachdeva (edited September 10, 2001).]
Regards<BR>Gurpreet Sachdeva<P>For Mock Exams, FAQ, Exam tips and some useful information about Bitshift operator, inner classes, garbage collection,etc please visit: <A HREF="http://www.go4java.lookscool.com" TARGET=_blank rel="nofollow">http://www.go4java.lookscool.com</A>
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.