Difference in if statements
Hey hey, what exactly is the different between
(someVariable == someValue)
(someVariable.equals(someValue))
(someVariable.equalsIgnoreCase(someValue))
7 Replies
⌛ This post has been reserved for your question.
Hey @ᴊᴏɴᴀꜱ! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./closeor theClose Postbutton 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.
== used with objects only compares by reference equality, e.g.
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)
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?
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.
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.Thanks. Makes sense, an intrger doesn't have upper numbers xd
Post Closed
This post has been closed by <@1351559309074501753>.