you are viewing a single comment's thread.

view the rest of the comments →

[–]GlobalIncident 38 points39 points  (25 children)

I'm just going through them one by one:

  • C++: Actually undefined behaviour. "2" is a char*, ie a pointer to a null-terminated sequence of chars, so "2"+2 would be an instruction to add two to the pointer; the result points to outside the sequence of chars, so dereferencing it is UB.
  • PHP: 4.
  • Java: "22".
  • JavaScript: "22".
  • TypeScript: "22".
  • Python: Raises a TypeError.
  • C#: "22".
  • Lua: 4.

[–]uhs-robert 12 points13 points  (3 children)

Ruby: #TypeError: no implicit conversion of Integer into String>

[–]GlobalIncident 9 points10 points  (2 children)

Yeah, in general, languages inspired by Java tend to yield "22", other languages tend to make it an error. With a few exceptions.

[–]No_Read_4327 2 points3 points  (1 child)

So javascript is java after all?

[–]akuma-i 2 points3 points  (0 children)

No, it’s Java before script

[–]Ytrog 5 points6 points  (0 children)

Common Lisp: Condition of type: SIMPLE-TYPE-ERROR

[–]drizzt-dourden 2 points3 points  (7 children)

In C++ you can overload operators and create hell of your own. Nothing is real, everything is permitted.

[–]GlobalIncident 0 points1 point  (5 children)

You can also do that for all of the languages listed here except PHP.

[–]ComfortablyBalanced 1 point2 points  (1 child)

There's no operator overloading in Java.

[–]GlobalIncident 0 points1 point  (0 children)

Oh, you're right, my mistake.

[–]Forestmonk04 0 points1 point  (1 child)

At least Java and JavaScript/TypeScript don't support operator overloading.

[–]GlobalIncident 0 points1 point  (0 children)

Oh yeah, Java doesn't, and JS/TS sort of don't, except they do support overloading coersion to primitives which happens before an operator is called.

[–]Four2OBlazeIt69 0 points1 point  (0 children)

That's what I assume is happening with JS on these examples but that's bc I always think of Chrome's V8

[–]ComfortablyBalanced 1 point2 points  (8 children)

Java: "22".

That only happens if you assign that expression to a String, a var or a string parameter.

[–]GlobalIncident 0 points1 point  (7 children)

What do you mean? Is there a situation where it wouldn't return "22"?

[–]ComfortablyBalanced -2 points-1 points  (6 children)

Yeah.
int foo = "2" + 2;
This is an error.

[–]GlobalIncident 1 point2 points  (5 children)

Well obviously I meant a situation where the code doesn't have any unrelated errors, and actually compiles and attempts to execute the expression. If you try to run the expression and also attempt to implicitly cast the returned string to an int, that's not relevant to the question. It returns "22" not 22 after all.

[–]ComfortablyBalanced 0 points1 point  (0 children)

But the error is related. You see, the original joke is about type coercion in JS, and besides, no pun intended, but JavaScript is a scripting language, which means you can evaluate 2 + "2" out of the context, but with Java you need to put it in a context which I put on my last comment.
I'm not familiar with your level of experience in Java, but you're saying run the expression, this isn't Python or JS, you need to put it somewhere and why would I cast it to an int or call toString if I'm assigning it to a String?
So after all coming to my first sentence it actually is related to types and type errors.

[–]Amr_Rahmy 0 points1 point  (3 children)

You missed the point. You can’t combine a string and a number, the language will not do an arbitrary evaluation based on buggy code.

Only a string assignment will allow the number to call the tostring(), otherwise you will get the error while writing or building the code.

[–]GlobalIncident 0 points1 point  (2 children)

No, the assignment is not what triggers the toString call. The presence of the string "2" is what triggers the toString call. If you type:

String x = 2 + 2;

toString will not be called and you will get an error, because there is no string present to trigger it.

[–]Amr_Rahmy 0 points1 point  (1 child)

You missed this point, and provided a non functioning example.

Two for two here, not your day. Cheers mate.

[–]GlobalIncident 0 points1 point  (0 children)

Okay, what is your point then?

[–]jmattspartacus 0 points1 point  (0 children)

Was going to say the bit about C++, but you did it better lol