In Java, there are two ways to check if two variables are the "same": `==` and `.equals()`. These two methods do not work the same, however.
## The `==` Operator
The basic equality operation in Java, `==` as in `var1 == var2`, checks whether `var1` and `var2` point to the same *object reference*.
That is, if `var1` is the same *instance* of a class in memory as `var2`, then `var1 == var2` is true.
However, if `var1` and `var2` were created as two separate instances of a class (i.e. with the `new` keyword), then `var1 == var2` will be false. Even if both objects happen to contain the exact same properties and values, the `==` comparison would not pass because they are not pointing to the same object in memory.
For primitive variable types, such as `int` and `double`, the `==` operator can always be used to check for equality, as their values are stored directly with the variable (rather than as a reference to another slot in memory).
### Note: a common first time coding error is to use a single equal sign instead of double equal signs. Using a single = will cause a compile time error. For example:
```java
int var1 = 4;
int var2 = 4;
if(var1 = var2)
System.out.println("The variables are equal.");
else
System.out.println("The variables are not equal.");
```
The `if` statement will cause an error. It is important to remember that a single equal sign is used for assignment while the double equal sign is used for comparison.
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.
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:
Most of the built-in classes in Java, as well as classes provided by popular libraries, will implement the `equals()` method in a meaningful way.
For example, the `java.util.Set` interface specifies that a `Set`'s `equals()` method will return true if "the specified object is also a set, the two sets have the same size, and every member of the specified set is contained in this set".
However, if a class does not override the default `equals()` implementation, the default implementation will apply, which simply uses the `==` operator to compare the two objects.
This built-in function in java is used to compare the equality of 2 strings return true or false depending on the match but this function does not see if the characters are in upper case or in lower case.