In Java 3 = 12(virtualspecies.com)
virtualspecies.com
In Java 3 = 12
http://www.virtualspecies.com/2017/06/in-java-3-12.html
21 comments
Same for C#:
Console.WriteLine(1 + 2 + "=" + 1 + 2);
writes 3=12
> 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.
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.
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.
> 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.
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.
Thanks for your comment. I mentioned System.out since I used it for printing.
Happens in Scala too:
scala> 1+2+"="+1+2 res1: String = 3=12
scala> 1+2+"="+1+2 res1: String = 3=12
Thanks for adding more fun.
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);
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);
Man you are forcing me to make another post in my blog. lol. By the way, the way you explain is perfect. Thanks.
From https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.htm...
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.
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.
Overloading + is a huge design mistake in many languages and a source of bugs.