Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

System.out.println method for boolean value with String compilation error

843789Apr 21 2010 — edited Apr 21 2010
Could anyone plz explain why this going to happen ?
why we need a bracket here for boolean in sysout, why it is not showing any compilation error ?

public class General {
public static void main(String[] args) {
String s1 = "Kamal";
String s2 = new String("Kamal");
String s3 = new String(s2);
String s4 = s1==s2; // compilation error here

System.out.println("s1==s2 : " + s1==s2 ); // expecting compilation error as above, but no compilation error here and in outputnot showing string 's1==s2'
System.out.println("s3==s2 : " + s3==s2); // same as above
System.out.println("s1==s3 : " + s1==s3); // same as above

System.out.println("s1==s2 : " + (s1==s2)); // this is working fine as expected
System.out.println("s3==s2 : " + (s3==s2));
System.out.println("s1==s3 : " + (s1==s3));
}
}
Output is :
false
false
false
s1==s2 : false
s3==s2 : false
s1==s3 : false
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 19 2010
Added on Apr 21 2010
6 comments
637 views