Ask coding questions

← Back to all posts
Printing double as integer if there are no decimals
farkhad (0)

Why "s1" and "s2" are different in the provided example code?

Commentshotnewtop
malvoliothegood (146)

When comparing int and float number types for equality the int is converted to a float. See: https://stackoverflow.com/questions/41653576/is-it-safe-to-compare-a-float-and-an-int-in-java

farkhad (0)

@malvoliothegood okay, still doesn't make sense in particular this example of code.
I mean v == v2 is always true, so I was expecting s1.equals(s2) == true, which is not.

malvoliothegood (146)

@farkhad
When I was researching your problem I came across some information that said when the == operator was used the tested values where compared by reference rather than value, whereas the equals function tests values. I guess you need to research if this is the case and how it applies to your code. I am not a Java expert and have not programmed using it for 20 years.