+ 3

== implicit data type conversion

So, in one of my code I compared a float and an int (say, 21.0 and 21) and 21.0 == 21 returned true. Now, I was expecting an error because of the difference in types but didn't get one. I have a therory: implicit data type conversion? Knew that arithmetic operators did this; was unaware it happened with == too. My question: is this a case of implicit data conversion or is there something else happening?

25th Mar 2018, 10:23 AM
cyk
cyk - avatar
2 Answers
+ 8
There is a diffetence between == and is. The expected behavior will occur on is https://code.sololearn.com/cAktb4Zqr02J/?ref=app
25th Mar 2018, 10:55 AM
Oma Falk
Oma Falk - avatar
+ 6
There is no implicit type conversion: == compares value identity is compares reference identity 21 references to integer and 21.0 references to double but the value of both is identical. Maybe deep under the surface there is a conversion to allow == operator for comparing double and integer.
25th Mar 2018, 11:25 AM
Oma Falk
Oma Falk - avatar