Difference in if statements

Hey hey, what exactly is the different between (someVariable == someValue) (someVariable.equals(someValue)) (someVariable.equalsIgnoreCase(someValue))
7 Replies
JavaBot
JavaBot6mo ago
This post has been reserved for your question.
Hey @ᴊᴏɴᴀꜱ! 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.
Madjosz
Madjosz6mo ago
== used with objects only compares by reference equality, e.g.
"a" == new String("a") // false
"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
"a".equals("A") // false
"a".equalsIgnoreCase("A") // true
ᴊᴏɴᴀꜱ
ᴊᴏɴᴀꜱOP6mo ago
Thanks for that great explanation! Do I get this right, the difference between .equals and equals Ignore Case is just that the second one doesn't care about uppercase / lowercase letters?
JavaBot
JavaBot6mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Madjosz
Madjosz6mo ago
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.
ᴊᴏɴᴀꜱ
ᴊᴏɴᴀꜱOP6mo ago
Thanks. Makes sense, an intrger doesn't have upper numbers xd
JavaBot
JavaBot6mo ago
Post Closed
This post has been closed by <@1351559309074501753>.

Did you find this page helpful?