> 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.
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.
Thanks for your comment. It wasn't a "magical thinking", well kind of. lol. Actually, I wanted to keep the post short. I think I need to change wording a little. By the way, your explanation is perfect.
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.
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.
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.
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.