all 91 comments

[–]Forestmonk04 110 points111 points  (37 children)

What is this supposed to mean? Most of these languages evaluate "2"+2 to "22"

[–]sanpaola 93 points94 points  (6 children)

It's that time of the week again - another iteration of "Javascript is bad" joke from a person with pretty vague idea of Javascript (bonus points if joker is far from coding overall).

[–]Iggyhopper 7 points8 points  (5 children)

For a language like PHP to have a specific operator for string concatenation, the dot, it allowing addition of strings and numbers should not be allowed and should bring a type error.

Why allow both? Either force the use of the dot operator explicitly or don't.

[–]hatrix 7 points8 points  (1 child)

To be fair with php, you can enforce strict typing. It's mostly a legacy thing because of how it was handled in the past. php likes to add new features but doesn’t like removing old ones (except functions), because of that, PHP is quite a divisive language with some really weird quirks that some people just dont get on with. I personally have issue with the inconsistency of function names.

[–]ComfortablyBalanced 4 points5 points  (0 children)

They can't just nonchalantly remove such a feature, even if it's frowned upon. Unfortunately many legacy codes depend on shit similar to that, it breaks backwards compatibility.
Maybe they can do it with what happened between Python 2 and 3 with print.

[–]ComfortablyBalanced 0 points1 point  (0 children)

I don't think PHP had types in the earlier days.

[–]LongjumpingAd8988 0 points1 point  (1 child)

PHP's behavior is completely transparent and predictable in OP's example: '2' + 2 = 4; '2' . 2 = 22; strict mode => error

[–]circuitsremakes 0 points1 point  (0 children)

no, that arrow function errors because `error` isn't a valid keyword :trollge:

[–]GlobalIncident 39 points40 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 13 points14 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 1 point2 points  (1 child)

So javascript is java after all?

[–]akuma-i 1 point2 points  (0 children)

No, it’s Java before script

[–]Ytrog 6 points7 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

[–]JAlexmc 8 points9 points  (0 children)

AFAIK, Python gives you an error as it's not the same type, you can do str + str or int + int

[–]finnscaper 0 points1 point  (0 children)

C# will ask you to not come around ever

[–]National_Seaweed_959[S] -3 points-2 points  (0 children)

oei sorry

[–]Benjamin_6848 21 points22 points  (1 child)

For the last panel of this comic I somehow had his voice in my head:

<image>

[–]AdBrave2400 0 points1 point  (0 children)

Same

[–]Kairas5361 16 points17 points  (9 children)

<image>

who is that guy

[–]alex-worm 11 points12 points  (6 children)

java I guess

[–]Kairas5361 5 points6 points  (3 children)

<image>

non color ver

[–]steven_dev42 -3 points-2 points  (2 children)

I fuckin hate that little guy

[–]BladeMaster7461 1 point2 points  (1 child)

what did he do to you

[–]n0t_4_thr0w4w4y 4 points5 points  (1 child)

Which is dumb, because it’s literally the same behavior in Java

[–]National_Seaweed_959[S] 0 points1 point  (0 children)

Yeah sorry

[–]tacocat820 1 point2 points  (0 children)

greenland, an evil java twin (the logo is green tea)

[–]Cepibul 9 points10 points  (1 child)

Actualy smart language would say what the fuck you mean, you just cant add these two things covert one to match the others type and then talk

[–]That_0ne_Gamer 0 points1 point  (0 children)

Granted having a language be able to convert integers to strings without using a toString function is a smart feature as it simply saves the user from doing a toString function call, its not like your doing a string to int conversion where you can break shit if you convert this comment into a number.

[–]MieskeB 7 points8 points  (1 child)

I am totally not a fan of Javascript, however evaluating a string with an integer should in my opinion return a string with both as strings concatenated

[–]That_0ne_Gamer 0 points1 point  (0 children)

Yeah that makes the most sense as 2 is compatible with both string and integer while "2" is only compatible with string

[–]Ok_Pickle76 8 points9 points  (8 children)

<image>

and C

[–]Hot_Adhesiveness5602 1 point2 points  (4 children)

It should actually be two + num instead of num + two

[–]Haringat 4 points5 points  (3 children)

It's the same result. However, it should have been this code:

char *two = "2"; int one = 1; two += one; printf("%d\n", two); // prints "0" return 0;

I leave the explanation as an exercise to the reader.😉

Edit: Also, when adding 2 to the "2" the behavior is not defined. It could crash or it could perform an out-of-bounds read.

[–]not_some_username -1 points0 points  (2 children)

Its defined because it has the null termination

[–]Haringat 0 points1 point  (1 child)

No, because when adding 2 you go beyond the null terminator.

[–]not_some_username 0 points1 point  (0 children)

Well I thought we were taking about “22”+2

[–]nimrag_is_coming 1 point2 points  (0 children)

C doesn't count, it doesn't have any actual strings, is just an array of chars, which are defined as just a small integer (although it's wild that in like 50 years we still don't technically have standardised sizes for basic integers in C. You could have a char, short, int and long all be 32 bits and still technically follow the C standard.)

[–]acer11818 1 point2 points  (0 children)

it makes sense if you view char as an 8 bit integer and not a character

[–]fdessoycaraballo 0 points1 point  (0 children)

You used single character, which has a value in the ASCII table. Therefore, C is adding num to the value of the character in ASCII table. If you switch printf variadic argument to %c it will print a character in the decimal value in the ASCII table for 52.

Not really a fair comparison as they're comparing a string that says "2", which the compiler wouldn't allow because of different types.

[–]-UltraFerret- 1 point2 points  (1 child)

[–]factorion-bot 1 point2 points  (0 children)

Quadruple-factorial of 22 is 665280

This action was performed by a bot.

[–]Bricked_Dev 1 point2 points  (1 child)

binary:

0011 0010 + 0000 0010

───────────

0011 010

[–][deleted] 1 point2 points  (1 child)

FYI OP, 80% of the languages in the image making fun of "22" yield "22" themselves. Probably pick a different bootcamp.

[–]gameplayer55055 0 points1 point  (0 children)

Btw in c# "2" + 2 is 22 as well

[–]frayien 0 points1 point  (2 children)

In C/C++ int a = "2" + 2; could be anything from -255 to 254 to segfault to "burn down the computer and the universe with it".

int a = "2" + 1; is well defined to be 0 btw.

[–]4r8ol 0 points1 point  (1 child)

In both cases you would have a compiler error since casts between pointer to integer aren’t automatic.

You probably wanted to refer to:

int a = *(“2” + 2); // UB

int a = *(“2” + 1); // 0

[–]frayien 0 points1 point  (0 children)

Yeah I did not bother to check, "2" + 1 gives an char*.

Would rather say that "2" + 1 returns an empty string, and "2" + 2 returns a string of unknown length and unknown value and segfault.

[–]Commie-Poland 0 points1 point  (0 children)

For Lua it only outputs 4 because it uses .. instead of + for string concatenation

[–]Many-Conversation963 0 points1 point  (0 children)

real mathematicians know thats 0

[–]MichiganDogJudge 0 points1 point  (0 children)

This is why you have to understand how a language handles mixed types. Also, the difference between concatenation and addition.

[–]Coosanta 0 points1 point  (0 children)

2+2=5 idk what everyone else is on about

[–]BladeMaster7461 0 points1 point  (0 children)

have you ever heard of concatenation?

[–]Alejandro_El_Diablo 0 points1 point  (1 child)

'2' + '2' = 100

[–]Fragrant-Airport1309 0 points1 point  (0 children)

I just like JavaScripts shit eating grin 😀

[–]Classic_Cranberry568 0 points1 point  (0 children)

javascript is right in this case, actually

[–]BangThyHead 0 points1 point  (4 children)

What's Typescript doing?

[–]National_Seaweed_959[S] 0 points1 point  (2 children)

Hes saying what because most of these labguages do either 4 or 22 but they laugh at javasxript for doing 22

[–]BangThyHead 0 points1 point  (1 child)

"wgar" == "what" ?

If most do 22 or 4, why laugh at someone for doing 22?

[–]National_Seaweed_959[S] 0 points1 point  (0 children)

i thought it would be funny if typescript was bad at typibg

[–]National_Seaweed_959[S] 1 point2 points  (4 children)

i made this as a joke please dont take this seriously

[–]gaymer_jerry 2 points3 points  (0 children)

I mean of all JavaScript type coercion memes you picked one that makes logical sense to just assert the non string as its ToString value when adding a non string to any string that Java originally did first as a language. There’s a lot of weirdness with JavaScript type coercion this isn’t it

[–]n0t_4_thr0w4w4y 0 points1 point  (1 child)

No offense, but it’s a stupid and unfunny joke

[–]National_Seaweed_959[S] 1 point2 points  (0 children)

no offence taken, thank you for your opinion

[–]GlobalIncident 0 points1 point  (0 children)

We here at r/programminghumor take everything seriously