In Rust, you'd simply declare if a structure is PartialEq or Eq.
If an object is PartialEq, then if two instances have the same value, they are still distinct instances and cannot be interchanged easily. (ie, two objects of the same value are not equivalent to two pointers pointing to a single value).
If an object is Eq, then two instances who share the same value are interchangable (ie, two objects of the same value are the same as two pointers to the same object)
In reality, very few objects need to be Eq, in most cases you can ignore two objects being the same reference. If you absolutely need it, you'll also need to live with the consequences, since the compiler can take Eq into account when optimizing.
Sorry for answering to old post, but I have to. PartialEq vs Eq dichotomy has nothing to do with reference equality. PartialEq means that values of that type with values of type which is a parameter of this trait (usually they are the same type, but, for example, there is an impl PartialEq<str> for String). However, it doesn't say much about properties of such equality. Most notably, the operation defined by PartialEq can be not reflexive (i. e. for any given value x of this type x == x may not hold). Eq serves the purpose of marker that states that equality is actually reflexive. The prime reason this dichotomy exists is floating point numbers. They have a special value, NaN (not a number), which is the only floating point number which is not equal to itself. This dichotomy is necessary because some code have to rely on this property in order to stay correct.
Put simply in Java and C# (a.k.a Microsoft Java) and a few other as well == only check if the two references point at the same object. To make sure they are actually equal you need to use equal method.
Example (Java):
var a = new Integer(500);
var b = new Integer(500);
System.out.println(a == b); // prints false
System.out.println(a.equals(b)); // prints true
b = a;
System.out.println(a == b); // prints true
It's worth pointing out that some languages - like Kotlin - can override operators. And because in Kotlins case method for overriding == operator is equals, all Java classes and objects can be compared using ==
edit: Wrote a tiny example in repl.it. Another note is record types, once actually implemented, should fix the hassle of having to declare == and such yourself. The proposal is here. F# already has them and it's pretty cool.
Java doesn't, unfortunately, have operator overload, but it works similar to C# - for references == compares references and only for primitives value.
In Java there is no need for extra interface (equals comes from Object class), but you need to also override hashCode method.
As a matter fact IntelliJ has 4 build in ways of generating equals/hashCode methods. In each it first compares references, then checks object type, then - depending on selected pattern - either compares fields one by one or uses some EqualsBuilder implementation.
Another similarity between these two languages is that Java is also getting records - Java proposal here in next version. Also to get rid of the hastle of declaring certain methods yourself.
Kotlin already has that, with data classes and can confirm - very cool :)
EDIT: Personally I think Java should have operator overloading, like C# and Kotlin, but unlike C# == should always call equals method (I don't see a point in having to implement custom equals method and override equals operator). As for reference comparison it should be ===
69
u/ThePyroEagle λ Feb 17 '20
a == bvs.*a == *b21
u/deanrihpee Feb 17 '20
Me : using the correct and arguably faster yet more longer way to compare strings in C# My small purpose program : this
249
u/AdmiralSam Feb 16 '20
Easy, just stop using Java and use a real language
27
u/grizzchan Feb 16 '20
IIRC there's a way to make Java use the equals method when using ==
6
u/Hell_Breaker Feb 17 '20
===
3
u/-Redstoneboi- Feb 17 '20
sauce
3
u/Cuckboy97 Feb 17 '20
Wait, that's a thing??
2
u/Ri_Konata Feb 17 '20
learning Java and yes, it's a pain ...
2
u/DeltaPositionReady Feb 17 '20
SQL team assemble!
^-=
2
u/2Tori Feb 17 '20
Dw just overload the operator c: