== or .equals

An inventory: what == compares for each kind of value in this course, where .equals is the question to ask instead, and which one Check.is uses.

== asks one question everywhere: do the two names hold the same thing? A number or a boolean sits in the name itself, so for those == compares the values. Everything else sits in the heap and the name holds its address, so == asks same address?, and two things made separately are never ==, however alike they look. .equals asks are they alike?, and each type answers it in its own way: some compare their parts, and some, having no answer of their own, say what == says.

The inventory

The value == compares .equals compares Use
int, double, boolean the values — ==
String addresses: two texts typed the same in the program share one; a text a method made has its own the letters .equals
StringBuilder addresses addresses too == for same builder?; for the text, a.toString().equals(b.toString())
an enum value, Day.SAT addresses, and there is only one Day.SAT the same ==
a record addresses: two news are two the parts, in order .equals
a Lst addresses: two Lst.of are two the elements, in order .equals
an Integer, a number taken out of a Lst<Integer> against an int, the numbers; against another Integer, addresses, which Java shares only for small numbers, so two equal numbers can come out false the numbers == against an int; .equals against another Integer
a Color addresses: Color.RED is one, Color.rgb(200, 0, 0) made twice is two the four parts .equals
an Img addresses addresses too neither: compare Img.width and Img.height
a Random addresses addresses too nothing to compare

The String row is the one to remember. "######" == "######" is true only because Java keeps one copy of a text typed twice in the program; "#".repeat(6) == "######" is false, and .equals is true for both. Ask .equals of a text every time, and the one case where == happens to work stops mattering.

The Integer row is the same trap one step on. A list can only hold things with addresses, so its numbers are Integer, not int: a number living in the heap. Next to an int, as in u == 10, it behaves as a number. Next to another Integer, as in u == v with both taken out of a list, == compares addresses, and Integer.valueOf(200) == Integer.valueOf(200) is false where 10 == 10 was true. Between two of them, ask .equals.

What Check.is does

Check.is picks for you, by the type of what it is handed: == for an int, a double and a boolean; .equals for a String, an enum value, a record and a Lst. There is no Check.is for two builders or two pictures: check a builder’s text, b.toString(), and a picture’s width and height.

The picture behind it

The directory holds a number in the name and an address for everything else; the heap holds the things. == reads the directory and nothing else. How Pyret runs your program draws the directory, and Class 10 drew the heap beside it for String and StringBuilder.