In Java 3 = 12
11–20 of 55 posts
Re: In Java 3 = 12
#12Re: In Java 3 = 12
#13(1+2)=3 then concat with string = then concat with 1 and concat with 2.
The optimiser converts these concats to a StringBuilder.
https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.htm...
Second time I've seen something like this on HN.
Re: In Java 3 = 12
#14Re: In Java 3 = 12
#15Re: In Java 3 = 12
#16- operator precedence
- implicit type casting
- how operator+ is defined per each type (e.g: numbers add, strings concatenate)
Re: In Java 3 = 12
#17The deeper question is: Was it a wise choice to disallow operator overloading BUT use "+" as a string concatination operator AND convert all values to strings during string concatenation?
I think not, but it's a rather moot point, since that desing decision is not likely to change...
Re: In Java 3 = 12
#18Re: In Java 3 = 12
#191) Left-associativity 2) The price you pay for type coercion. Be thankful Java defines the order of evaluation. Not all languages which coerce specify the order of evaluation.
Yes but that's strange. Java is such a staticaly typed language you would not expect it to do that if you are like me and don't know it very well. While Python is very dynamic, but: >>> print(1 + 2 + "=" + 1 + 2) Traceback (most recent call last): File " ", line 1, in TypeError: unsupported operand type(s) for +: 'int' and 'str'
Re: In Java 3 = 12
#201) Left-associativity 2) The price you pay for type coercion. Be thankful Java defines the order of evaluation. Not all languages which coerce specify the order of evaluation.
Yes but that's strange. Java is such a staticaly typed language you would not expect it to do that if you are like me and don't know it very well. While Python is very dynamic, but: >>> print(1 + 2 + "=" + 1 + 2) Traceback (most recent call last): File " ", line 1, in TypeError: unsupported operand type(s) for +: 'int' and 'str'
Python, on the other hand, does not need to do a typing pass at parse-time. Instead, literals are stored as values, and those values are tagged with their source class. Python's way of doing it means that a + operation is called upon with an integer and a string.
Java might be able to yield the same kind of result as Python if it had operator overloading. It could define Integer's + operator as only taking other Integer values, therein yielding a type error at parse-time.
My own language uses ++ instead of + to distinguish between addition and concatenation, though I've considered some other token since the two are quite similar to each other.