Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

15
  • 1
    "Java doesn't support operator overloading, which means == only applies to primitive types or references. Anything else requires invocation of a method.": One could add that if == meant string equality, we would need another notation for reference equality. Commented Apr 2, 2013 at 11:03
  • @Giorgio: Exactly. See my comment on Gilad Naaman's answer. Commented Apr 2, 2013 at 11:05
  • Although that can be solved by a static method that compares the references of two objects (or an operator). Like in C#, for example. Commented Apr 2, 2013 at 11:08
  • @GiladNaaman: That would be a zero-sum game because it causes the opposite problem to what Java has now: equality would be on an operator and you'd have to invoke a method to compare references. Further, you'd have to impose the requirement that all classes implement something that can be bound to ==. That's effectively adding operator overloading, which would have tremendous implications on the way Java is implemented. Commented Apr 2, 2013 at 11:16
  • 1
    @Blrfl: Not really. There will always be a defined way to compare reference (ClassName.ReferenceEquals(a,b)), and a default == operator and Equals method both pointing to ReferenceEquals. Commented Apr 2, 2013 at 11:29