For example, if you have an Integer object (not a primitive int) you should use .equals() and not == for comparison. The == is used for comparing references and not values.
That said, this only illustrates some extra complexity in Java.
For example, if you have an Integer object (not a primitive int) you should use .equals() and not == for comparison. The == is used for comparing references and not values.
That said, this only illustrates some extra complexity in Java.
Integer a = new Integer(10);
Integer b = new Integer(10);
Integer c = new Integer(1000);
Integer d = new Integer(1000);
a==b is true
c==d is false
Python 2.6.6 (r266:84292, Oct 17 2010, 15:53:05)
[GCC 4.4.3] on linux2
>>> a = int(10)
>>> b = int(10)
>>> c = int(1000)
>>> d = int(1000)
>>> a is b
True
>>> c is d
False
Same reasoning as Java?
Thinking about it now, I would rather the inverse in a language be true, and have == be used for value comparison, and have a more wordy function for reference comparison. In my experience, I feel that its a much more common usage, and would probably reduce errors caused by programmers choosing the wrong operator. The only downside to this is that you then have to override an operator if you want to change equality comparison, instead of a function.