Reply to post: Re: so wrong

Oracle's Java 15 rides into town, waving the 'we're number one' flag, demands 25th birthday party

james_smith Silver badge

Re: so wrong

"It's definitely quirky to reuse a well established and well known equality test operator, and assign it a new meaning. Compare to python, where == is the equality test, and is is the "same object" test."

If by "same object" you mean contains the identical data, then in Java that would be conflating two different things - as I point out in my previous post, there is reference equality and logical equality. This is entirely consistent with C/C++, which influenced Java along with Smalltalk. Take the following class:

public class Foo {

public int i;


You create instances and assign their reference to variables like so:

var foo = new Foo();

var bar = new Foo();

In both instances, the member variables i are initialised to zero by default. Comparing foo and bar with the == operator compares the references, so the following evaluates to false:

boolean b = foo == bar; // false

However, assigning the same reference to another variable and comparing them with the == operator evaluates to true:

var baz = foo;

boolean b = foo == baz; // true

If you want to compare the instances for logical equality, in other words whether their member variables i hold the same value, then you need to override the equals() method. You'd then compare the instances as follows:

boolean b = foo.equals(bar);

This confusion probably stems from the sort of, kinda misleading claim in the early days of Java that it doesn't have pointers. It doesn't have them in the same way C/C++ does, with all the potential pitfalls that brings, but it does have references similar to the way C++ does - albeit minus the & syntax for method arguments.

POST COMMENT House rules

Not a member of The Register? Create a new account here.

  • Enter your comment

  • Add an icon

Anonymous cowards cannot choose their icon


Biting the hand that feeds IT © 1998–2020