java - Why System.out.println("hey s1==s2:"+s1==s2); prints "false" as the output instead of printing "hey s1==s2:false" -
i have written next java code:
string s1 = new string("shan"); string s2 = new string("shan"); string s3="shan"; string s4="shan"; system.out.println("hey s1==s2:"+s1==s2); system.out.println("s3==s4:"+s3==s4); system.out.println("s1.equals(s2): "+s1.equals(s2)); system.out.println("s3.equals(s4): "+s3.equals(s4)); system.out.println("s1==s3: "+s1==s3); system.out.println("s1.equals(s3): "+s1.equals(s3)); system.out.println("hey s1==s2:"+true);
the output:
false false s1.equals(s2): true s3.equals(s4): true false s1.equals(s3): true hey s1==s2:true
why line #5 result in "false" output instead of "hey s1==s2:false"?
line5: system.out.println("hey s1==s2:"+s1==s2);
because of operator precedence "hey s1==s2:"+s1
resolving first , comparing s2 leads false.
give highest precedence resolve correct. parenthesis have highest precedence.
system.out.println("hey s1==s2:"+(s1==s2));
java string
No comments:
Post a Comment