Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
@Kushtrim Yeap, the problem is that it will ALWAYS return true. Even this in Java is true:
"Trump" == "Hillary" -
== compares object to object so its only true if the two are the same object.
Because string literals usually cached, they appears to be the same object. Hence 'a' == 'a' is true but 'a' == 'b' is false.
.equals on the other hand compares object by value.
And for 'Why not use == instead of .equals and === for object comparsion?'
Because you might want to override equality but Java dont have operator overloading, so .equals was the solution. -
As far as I know thats only for strings and only because + compiles to StringBuilder. But you cant define custom overloads
-
Java doesn't have === because it doesn't need to. == compares type as well. 1 =='1' is false.
-
@jirehstudios the point is depending on how it is built
When using == in java Two different string objects whose values are the same, would return false. -
@brettmoan would you ever have two different string literal with the same value? I thought all references to strings with the same value would be pointing to the same strings?
-
== compares type in most languages, and === compares type and identity in most languages.
-
@MindfulMinun just to be clear, it does compare value as well right? I mean in general "foo" == "bar" would not be true just because they share the same type, right?
-
java91588y@siljamicke == only compares the reference of an object. It completely ignores the value and returns true if both sides refer to the same object.
It does compare values if you use primitives though. -
@java9 i was speaking in general terms. I guess what you're saying is correct in Java. But how on earth would "foo" == "bar" ever evaluate to true in any language?
-
java91588y@siljamicke sorry, you are right about that of course! I for sure wouldn't understand why.
-
@java9 read the above with respect to what was said further above, that the == operator compares type. If that would be the case anystring == anyotherstring should evaluate to true. I find that hard to believe...
-
java91588y@siljamicke Yes I think type isn't quite the right word to use there. I assume the intended meaning was value.
-
I'd love to know where the idea of '== compares types' came from. It's not even close! For anyone who wants to know the truth - http://stackoverflow.com/questions/...
Related Rants
-
Cyborg15A guy and a girl are in a Java seminar. Afterward, the guy approaches the girl and asks, "Hey gurrl, can I ge...
-
linuxxx32*client calls in* Me: good morning, how can I help you? Client: my ip is blocked, could you unblock it for m...
-
DRSDavidSoft28Found this in our codebase, apparently one of my co-workers had written this
Been programming Java for a few weeks now, and WTF is this, Java?!
"Example".equals("Example")
What is wrong with the form that a dozen of other languages use?
"Example" == "Example"
P. S. If you don't know Java, the latter one compares for the type of objects and always returns true in this case.
undefined
java
wtf