From the article:
Originally Posted by Common Java Mistakes
To add a little more to this, most objects in Java are referenced by their memory address. When you use the == operator, you're comparing the addresses of two objects. So if you have two distinct object which happen
to have the same value, would still have different memory addresses, and == would return false. If you want to test if two objects just happen to have the same value, you use the .equals() method.
Note that for primitives (int, boolean, byte, float, etc.) there's no need for a .equals() method because these don't contain a reference to the value, but rather contain the value itself. However, if you're using wrapper classes (Integer, Float, Boolean, etc.), you must use the .equals() method because now you're holding references to the wrapper object, not the direct value.
.equals() is used because Java doesn't have operator overloading. Python does, so you could overload == to do what the .equals() method does in Java. To allow Python users to still compare memory addresses, Python has the is
keyword, which is analogous to Java's == operator.
I've never used VB
, so I couldn't say what the equivalent VB
code would be