
In Java 3 = 12 - sayham28
http://www.virtualspecies.com/2017/06/in-java-3-12.html
======
Terr_
> Because Java starts treating everything as a String once it has found a
> string in System.out statement

Uh, no, I suspect this is an example of "magical thinking" by the author.

Far more logical theory: It's working left-to-right, doing int-to-int addition
until it hits the first string, which is "=", at which point it is locked into
doing string-to-string concatenation for the remainder.

String concatenation is one of the few places Java has overloaded operators,
and this quirk (and reader confusion!) is an fair example of why Java does NOT
allow users to overload operators for their own classes.

~~~
danielmg
optimiser changes string concat to StringBuilder.

~~~
sebazzz
While true, this is not relevant. The simple fact is left to right evaluation
and whether a StringBuilder is used, concatenation, constant computation or
creating a Char array and joining the characters is not relevant.

------
dragonwriter
> Because Java starts treating everything as a String once it has found a
> string in System.out statement

No, because int + int -> int, int + string -> string, and string + int ->
string.

System.out is irrelevant, except that it's what causes the result of the
computation to be printed.

~~~
sayham28
Thanks for your comment. I mentioned System.out since I used it for printing.

------
danielmg
From
[https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.htm...](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.18.1)

To increase the performance of repeated string concatenation, a Java compiler
may use the StringBuffer class or a similar technique to reduce the number of
intermediate String objects that are created by evaluation of an expression.

The + operator is syntactically left-associative, no matter whether it is
determined by type analysis to represent string concatenation or numeric
addition. In some cases care is required to get the desired result.

------
bmn__
In Perl, no such surprises, because the operators for string concatenation and
arithmetic addition are different, as they should be. Motto: different things
should look different.

Overloading + is a huge design mistake in many languages and a source of bugs.

    
    
        $ perl -E'say 1 + 2 . "=" . 1 . 2'
        3=12
        $ perl -E'say 1 + 2 . "=" . (1 + 2)'
        3=3
        $ perl6 -e'say 1 + 2 ~ "=" ~ 1 ~ 2'
        3=12
        $ perl6 -e'say 1 + 2 ~ "=" ~ 1 + 2'
        3=3

------
vbcr
and easily mitigated by the right use of parentheses.

System.out.println((1 + 2) + " = " \+ (1 + 2));

~~~
sayham28
Right but where is fun in that?? Lol. By the way, I think I should add this
part on the post to show the fix, especially for the newcomers in Java world.

------
okasaki
Interestingly, Python is more "strongly typed" in this sense

    
    
        TypeError: unsupported operand type(s) for +: 'int' and 'str'

~~~
33degrees
As is Ruby:

    
    
        TypeError: String can't be coerced into Fixnum

------
hdkesting
Same for C#: Console.WriteLine(1 + 2 + "=" \+ 1 + 2); writes 3=12

------
gkanapathy
Happens in Scala too:

scala> 1+2+"="+1+2 res1: String = 3=12

~~~
sayham28
Thanks for adding more fun.

------
danielmg
What's happening is this

1+2 = 3

Then hits string

Optimiser changes + to String builder

So you get

StringBuilder sb = new StringBuilder();

sb.append(3);

sb.append("=");

sb.append(1);

sb.append(2);

~~~
sayham28
Man you are forcing me to make another post in my blog. lol. By the way, the
way you explain is perfect. Thanks.

~~~
danielmg
[https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.htm...](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.18.1)

