2
String literal1 = "java"; 
String object = new String("java"); 
String literal2 = "java";

System.out.println("result 1 = " + (literal1 == object) ); 
System.out.println("result 2 = " + literal1.equals(object)); 
System.out.println("result 3 = " + literal1 == object); 
System.out.println("result 4 = " + literal1.equals(object));
System.out.println("result 5 = " + literal1 == literal2); 
System.out.println("result 6 = " + literal1.equals(literal2));

Expected output

result 1 = false
result 2 = true
result 3 = false
result 4 = true
result 5 = false
result 6 = true

output obtained

result 1 = false
result 2 = true
false
result 4 = true
false
result 6 = true

When this line

System.out.println("result 5 = " + literal1 == literal2);

is changed to

System.out.println("result 5 = " + (literal1 == literal2));

Output

result 5 = true 

Could anyone please explain why this is happening?

Yash
  • 11,486
  • 4
  • 19
  • 35
Aksh
  • 71
  • 1
  • 7

3 Answers3

3

It happens because expressions are evaluated left-to-right so it will first concatenate your string (i.e. "result 3 = " + literal1) and then check for truthiness (i.e. == object), hence printing only false because the result of the concatenation is not of the same value as object.

In the first (and last) example ("result 1 = " + (literal1 == object)) you direct the default evaluation with brackets forcing (literal == object) to evaluate separately before the concatenation which is why it prints false only for that evaluation, concatenated with the string preceding it.

zwer
  • 24,943
  • 3
  • 48
  • 66
  • Ha! This would be a neat question for some "intro to java" course. – flakes Jul 14 '18 at 06:24
  • Sorry, I have to down-vote because of the blatantly false statement in the first line. `+` is evaluated before `==` because of *operator precedence*, not because of left-to-right *evaluation order*. – Andreas Mar 01 '21 at 02:43
2

TLDR: it's precedence, not left-to-right

Java does have a rule that operands are evaluated left-to-right, but that has no effect here.

Also in Java all binary (meaning two-operand, not bitwise) operators other than assignment are left-associative, but that does not apply here because associativity only matters when operators have the same precedence.

What matters here is that + has higher precedence than == so as VietDD says

System.out.println("result 5 = " + literal1 == literal2);
# is equivalent to
System.out.println(("result 5 = " + literal1) == literal2);
# which is false because they aren't the same object

which happens to be the same as grouping to the left.

But if we consider instead

System.out.println(literal1 == literal2 + " is result 5!");
# THAT is equivalent to
System.out.println(literal1 == (literal2 + " is result 5!"));
# ditto

which happens to be the same as grouping to the right.

dave_thompson_085
  • 34,712
  • 6
  • 50
  • 70
1

System.out.println("result 3 = " + literal1 == object);

System.out.println("result 5 = " + literal1 == literal2);

is equivalent to

System.out.println( ( "result 3 = " + literal1 ) == object);

System.out.println( ( "result 5 = " + literal1 ) == literal2);

It's String Concatenation

  1. The expression is evaluated left to right.
  2. If either operand is a String, + means concatenation

You can try this :

System.out.println( 1 + 2 + "3");

Output :

33

1 + 2 = 3

3 + "3" = "33"

And

System.out.println( "1" + 2 + 3);

Output:

123

"1" + 2 = "12"

"12" + 3 = "123

Community
  • 1
  • 1
VietDD
  • 1,048
  • 2
  • 12
  • 12
  • *"The expression is evaluated left to right."* Misleading: The *operands* are evaluated left-to-right, but the *operators* are evaluated according to their *precedence* and *associativity*. – Andreas Mar 01 '21 at 02:47