user448363
user448363

Reputation: 81

Why do we need this special === operator?

I've used C++ and Java before and they don't have this === operator.

How come they manage without it but in languages like PHP its key.

Upvotes: 5

Views: 264

Answers (4)

user187291
user187291

Reputation: 53940

when we say "A is equal to B" this can mean several quite different things

  • A and B are the same thing
  • A and B have the same value, that is, their values are not distinguishable for a third party
  • A and B can be converted to strings (or numbers) that are equal
  • A and B have the same hash value

etc

most languages do have different operators or functions for different kinds of equality , see http://en.wikipedia.org/wiki/Equality_%28relational_operator%29#Object_identity_vs._Content_equality

Upvotes: 2

Alex
Alex

Reputation: 7374

Just so you know, this is the same in JavaScript and according to JSLint should ALWAYS be used as a type-check, which technically makes == redundant. But I guess that's just personal preference

Upvotes: 0

sepp2k
sepp2k

Reputation: 370212

Actually equals in Java and == in C# act like === does in php. I.e. "24".equals(24) will return false.

What java and C# don't have an equivalent of is PHP's == (i.e. an operator/method such that "24".fuzzyEquals( 24 ) would return true). And that's because C# and Java are strongly typed and such an operator would be against their philosophy.

Upvotes: 10

Joachim VR
Joachim VR

Reputation: 2340

Because PHP is not type safe. == compares 2 values, but === compares the values AND checks if their class types are the same.

I believe "2" == 2 returns true, while "2" === 2 returns false.

Upvotes: 3

Related Questions