From the course: Java 8+ Essential Training: Syntax and Structure

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Compare String values with methods

Compare String values with methods - Java Tutorial

From the course: Java 8+ Essential Training: Syntax and Structure

Start my 1-month free trial

Compare String values with methods

- [Instructor] There are many scenarios where you need to compare strings to each other. Using simple equality operators, that is the equals equals operator, as you might do with numbers, doesn't work the way you expect. Here's an example, I've created two string variables named s1 and s2. I'll use a bit of conditional code, I'll type if, then press control shift enter, or command shift return, and that expands to the complete if clause. Now I need to pass in a conditional expression inside the parentheses. I'll use s1 double equals s2. That's the equality operator, and with primitive numbers it's always accurate. Now, if that condition is true, I'll do a little bit of console output. I'll use S out, and I'll output the string "they match". Then I'll move the cursor down here to after the if clause, and type else, once again I'll auto complete my code. And this time, I'll output the string "they don't match". Then I'll run this code. As you would expect, I get the result "they match".…

Contents