From the course: Java Cheat Codes: Most Popular Functions
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Solution: equals(Object obj) - Java Tutorial
From the course: Java Cheat Codes: Most Popular Functions
Solution: equals(Object obj)
(upbeat music) - [Instructor] Let's look at how I would implement the equals method to solve this question. Scrolling down, looking at lines 14 through 19 are my proposed solution. So starting off on line 15, the code passes str1 and str2 through the compareStrings, and then line 16, the equals method that we have here, which is specifically designed for string comparisons, compares to actual content or characters of the two strings, str1 and str2, and you are returning a Boolean value. So the equal method returns true if the strings have the same content, indicating they're considered equal. Otherwise, it returns false, implying they're not equal. So let's go ahead and try out this solution. Test my code. Here we go. My result is true. Another one solved. And what we see in the solution here is that it's concise and straightforward. We're leveraging the built-in functionality of the equals method. And the equals method is case-sensitive, meaning that if your string had a lowercase…