#Difference in if statements

1 messages · Page 1 of 1 (latest)

mental hinge
#

Hey hey, what exactly is the different between

(someVariable == someValue)
(someVariable.equals(someValue))
(someVariable.equalsIgnoreCase(someValue))

zinc parrotBOT
#

This post has been reserved for your question.

Hey @mental hinge! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

molten kettle
#

== used with objects only compares by reference equality, e.g.

"a" == new String("a") // false

because they both are different objects in memory.
obj1.equals(obj2) calls the equals() implementation of obj1, which for Strings checks if they contain the same characters in the same order.
equalsIgnoreCase() is a method of the String class which checks if both Strings contain the same characters using the UTF-16 or Latin-1 casing rules to identify uppercase with lowercase charactes. It does not take Locales into account (e.g. dotless vs dotted i are all seen the same)

"a".equals("A") // false
"a".equalsIgnoreCase("A") // true
mental hinge
zinc parrotBOT
molten kettle
#

Yes, just as the method name says. Note that equals is a general method on any Object though while equalsIgnoreCase is only available on Strings.

mental hinge