4 ms·
This isn't at all surprising if you know how Java works; you could probably point out this kind of a strange feature case in nearly any language if you look har
by brobdingnagians 9y ago
This isn't at all surprising if you know how Java works; you could probably point out this kind of a strange feature case in nearly any language if you look hard enough.
It does left to right order of operands, so 1 + 2 happens as integer addition since both operands are integers, then + "=" does string concatenation since one of the operands is a String, then "3=" + 1 evaluates as a concatenation since one of the operands is a String, giving "3=1" + 2, which evaluates to "3=12" by the same logic. Parenthesis solves this by explicitly specifying order of operations.
And explicitly specifying order of operations is almost always a good idea for making sure it does what you want and maintenance purposes.
- Peaker 9y ago> you could probably point out this kind of a strange feature case in nearly any language if you look hard enough I disagree. I think it's a product of bad language design.
- guelo 9y agoThe bad design here is having implicit type conversion at all. The few characters you save with this rarely used feature are not worth the potential confusion. But it is a trade-off, Java is normally criticized in the other direction, for forcing you to be overly explicit.
- reacweb 9y agoI disagree. I am not an expert of java language subtleties, but I had no problem to predict and explain this output. I think it is a product of the language simplicity. I have a deeper knowledge of C and more experience, but I regularly fail to predict output in similar exercises.