2
String a = "abc"; String b = "abc"; System.out.println("Result .... " + a==b); // false System.out.println(a==b); // true 

1st print statement prints false and 2nd prints true, though ideally it has to be true. Why is it false in 1st print statement ?

4
  • 2
    You do know not to compare string with == right? Commented Jan 18, 2016 at 5:42
  • @John3136 I know that very well. Better understand the question scenario. Commented Jan 18, 2016 at 5:44
  • Good stuff. Just as soon as I see string == string the alarm bells start to go off... Commented Jan 18, 2016 at 5:46
  • 1
    Why ask same question: stackoverflow.com/questions/34848089/…? Are posted answers not clear? Could you explain what other informations you want to get? Commented Jan 18, 2016 at 6:19

6 Answers 6

4

System.out.println("Result .... " +a==b); -> the result string will be appended with 'a' and then compares with b so it results false.

Sign up to request clarification or add additional context in comments.

Comments

4

Order of operations:

"Result .... " + a==b 

is equivalent to

("Result .... " +a) == b 

which will be false since the two strings are not the same reference.

The explanation for this is that the + addition operator has a higher precedence than == logical equivalence.

The expression a == b is returning true in your second statement due to interning, in which a and b actually refer to same string object.

Click here for a link to Oracle's table of operator precedence in Java.

Comments

2
  1. Forgot checking equality by == in java. In Java this operation checks equality of object link. Additionally it is applicable for checking equality of simple numbers. You should use .equals method

    String a = "abc"; String b = new String(a); System.out.printLn(a == b);//false System.out.println(a.equals(b));//true 
  2. Learn about operation order in java

Comments

0

It is because in "Result .... " +a==b it first add "Result .... " with a and then == to b. If you write like this "Result .... " +(a==b), then it will be OK.

Comments

0

In the first statement, you're comparing "Result .... " + a with b. In the second one, you're comparing a with b, hence the difference. Change your first statement as follows:

 System.out.println("Result .... " + (a==b)); 

And keep in mind that strings should be compared using the equals() method instead of ==.

Comments

0

System.out.println("Result .... " +a==b);

String Result is appended with 'a' and then compares with b so it provides false. (Result .... a == b) which is false.

Follow this link to understand precedence of operators in java and this.

Operator + has more precedence than == operator. 

try adding brackets, you will see the diff. Bracket is evaluated separately.

public static void main(String[] args) { String a = "abc"; String b = "abc"; System.out.println("Result .... " + (a == b)); // false System.out.println(a == b); // true} } 

output

Result .... true true 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.