Hi everybody, What should be the output of the following code? The answer given is wrong in my opinion. Please help. Q. If you compile the following code what will be the result? public class MyClass { public static void main(String args[]) { String str1 = "Test One"; String str2 = new String("Test One"); if ( str1.equals( str2) ) { System.out.println("Both are equal"); } boolean b = true; boolean b1 = false; if ( b.equals(b1) ) { System.out.println("true"); } } } [a] Compile time error [b] Runtime error. [c] No output [d] "Both are equal" followed by "true"
Thanx Jyotsna
Haining Mu
Ranch Hand
Joined: Jun 01, 2001
Posts: 51
posted
0
Originally posted by Jyotsna Umesh: Q. If you compile the following code what will be the result? public class MyClass { public static void main(String args[]) { String str1 = "Test One"; String str2 = new String("Test One"); if ( str1.equals( str2) ) { System.out.println("Both are equal"); } boolean b = true; boolean b1 = false; if ( b.equals(b1) ) { System.out.println("true"); } } } [a] Compile time error [b] Runtime error. [c] No output [d] "Both are equal" followed by "true"
Compile error, equals() method can not be used for primitive. though first part will be "equal"
Jyotsna Umesh
Ranch Hand
Joined: May 09, 2001
Posts: 94
posted
0
Originally posted by Haining Mu: Compile error, equals() method can not be used for primitive. though first part will be "equal"
Thanks Haining, i just missed it. Jyotsna
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12271
1
posted
0
Another case where writing a test case and trying to compile it would have answered your question faster than posting to JavaRanch. The best way to learn Java is to write LOTS of test cases. Bill