Home Help Center

Why no .equals() for Java classes

mefostermefoster Member Mary Ellen FosterOrganization: Heriot-Watt University, EdinburghProject: ECHOES learning environment ✭✭✭
The Java mapping for structs (section 10.7 of the manual) includes a .equals() method that compares member variables; however, the mapping for classes (section 10.14) doesn't create a .equals() method. Is there a reason for that?

NB: I'd like to be able to do comparisons between objects without having to write my own static comparison method, and I'd like to use classes rather than structs in Slice because of inheritance.

Thanks!

MEF

Comments

  • marcmarc FloridaAdministrators, ZeroC Staff Marc LaukienOrganization: ZeroC, Inc.Project: The Internet Communications Engine ZeroC Staff
    Slice classes conceptually have reference semantics. For example, they can refer to other class instances, to themselves, or they can be null. If you compare two class references, you get "true" only if both references refer to the same class. Structs, on the other hand, are conceptually value types. In C++, this is much more obvious than in Java, because in C++ structs can be passed by value.
Sign In or Register to comment.