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 ?

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
ShruR
  • 53
  • 1
  • 3
  • 2
    You do know not to compare string with `==` right? – John3136 Jan 18 '16 at 05:42
  • @John3136 I know that very well. Better understand the question scenario. – ShruR Jan 18 '16 at 05:44
  • Good stuff. Just as soon as I see `string == string` the alarm bells start to go off... – John3136 Jan 18 '16 at 05:46
  • 1
    Why ask same question: http://stackoverflow.com/questions/34848089/java-print-statement-only-prints-the-result? Are posted answers not clear? Could you explain what other informations you want to get? – Pshemo Jan 18 '16 at 06:19

6 Answers6

4

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

Shriram
  • 4,343
  • 8
  • 37
  • 64
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.

Community
  • 1
  • 1
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
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

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Pavel Uvarov
  • 1,090
  • 1
  • 10
  • 16
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.

Bahramdun Adil
  • 5,907
  • 7
  • 35
  • 68
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 ==.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
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
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116