From c7f8f8f75a9fd41b23408a561a523ad19da9cce5 Mon Sep 17 00:00:00 2001 From: BoopMehNose <33704699+BoopMehNose@users.noreply.github.com> Date: Sat, 23 Mar 2019 06:36:56 -0400 Subject: [PATCH] Exemplification changes (#33903) * Exemplification Provided more comments on the coding portions to show what each line does in respect to the program. Also specified what happens when "==" is used when the "equals()" method should be employed. * Formatting --- guide/english/java/equality/index.md | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/guide/english/java/equality/index.md b/guide/english/java/equality/index.md index ddc010af12..f7269acd5b 100644 --- a/guide/english/java/equality/index.md +++ b/guide/english/java/equality/index.md @@ -47,7 +47,7 @@ The `if` statement will cause an error. It is important to remember that a sing The built-in `Object` class in Java, which all other classes automatically extend, contains a number of helpful built-in methods. One such method is `equals()`, which takes another object as its argument and returns whether the two objects should be considered "equal" according to the relevant logic for that class. -The 'String' class is one of the most common examples of a class that overrides the `equals()` method. When comparing two 'String's for equality, you need to use the `equals()` method, as `==` won't work as you expect. +The 'String' class is one of the most common examples of a class that overrides the 'equals()' method. When comparing two 'String's for equality, you need to use the 'equals()' method, as '==' won't work as you expect. Instead of returning 'true' when comparing objects, the '==' operator will continuously return 'false' unless the objects being compared are the *same object*. ```java String s1 = "Bob"; @@ -59,16 +59,18 @@ System.out.println(s1.equals(s2)); //true When you create a new class in Java, you will often want to override the `equals()` method in order to provide a more meaningful way to compare two objects of the same class. How this method is implemented is completely up to the developer's judgement. -For example, you may decide that two `Person`s should be considered "equal" if their `name` and `dateOfBirth` are the same. This logic would be implemented in your `Person` class's `equals()` method: +For example, you may decide that two `Person` objects should be considered *equal* if their `name` and `dateOfBirth` instance variables are the same. This logic would be implemented in your `Person` class's `equals()` method: ```java public class Person { - public String name; - public Date dateOfBirth; - - public boolean equals(Person person) { - return this.name.equals(person.name) && this.dateOfBirth.equals(person.dateOfBirth); - } + public String name; + public Date dateOfBirth; + //The two lines above are instance attributes of the Person object + + public boolean equals(Person person) { + return this.name.equals(person.name) && this.dateOfBirth.equals(person.dateOfBirth); + //An edited equals() method can allow for the developer to choose what characteristics of an object should be compared in order to deem them equal + } } ```