Ask coding questions

← Back to all posts
Printing double as integer if there are no decimals

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


When comparing int and float number types for equality the int is converted to a float. See:


@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.


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.