Yes, there is a time to use ==
instead of equals
method. Typically the equality method checks the usual output of the class. For example string containing the word "cat" so when you use the equality method that is what it checks. However a class is much more than just a single field, but many fields as well as an address in memory. When using ==
on a class it literally checks if it is pointing to the same instance in memory. Only with primitive types does ==
do what you think it does.
Here is a really cool experiment to demonstrate...
String a = "123";
String b = "123";
The compiler optimizes the strings to the same address and so == will return true. However change the value of b at run-time EVEN TO 123 and == will return false because now they have a different address in memory because the virtual machine is not smart enough or it would not be efficient to do these kind of optimizations at run-time.
Scanner input = new Scanner(System.in);
b = input.nextLine();
Even if you enter 123 it will cause the == to return false because now it created a new spot in memory.