From the course: Java 8 Essential Training

Unlock the full course today

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

Comparing string values

Comparing string values - Java Tutorial

From the course: Java 8 Essential Training

Start my 1-month free trial

Comparing string values

- There are many scenarios where you need to compare strings to each other. And using simple equality operators as you might do with numbers, doesn't work the way you'd expect. In this project, CompareStrings, I've declared two string variables. They both have the same value, a string of Hello. I'm going to add a bit of conditional code. In Java you can write some simple conditional code with the if statement. The if statement places a boolean condition inside a pair of parentheses. And I'll compare str1 to str2 using the == operator. That's called the equality operator and it works great with primitive numbers, booleans, and characters. With strings it might appear that it works also. If this condition is true, I'll output a string to the console of They match! Then I'll add an else clause after the if clause and in that section I'll output They don't match! Then I'll run the code. Everything seems to match expectations. The two strings are identical and I get back They match! But…

Contents