From the course: Java 11+ Essential Training

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

Compare string values - Java Tutorial

From the course: Java 11+ Essential Training

Start my 1-month free trial

Compare string values

- There are many scenarios where you need to compare strings to each other. And using simple equality operators looks like it should work, but it doesn't work the way you might expect. I'm starting in this exercise with two string variables. One declared with explicit typing, the other with an inferred type. They both have the same value of hello and an exclamation mark. I'm going to create an if statement. In Java the if keyword is followed by a pair of parentheses, and the parentheses need to wrap an expression that evaluates as true or false. I'm going to be doing this the wrong way. I'll say if s-one matches s-two, using the double equals operator, the double equals operator works for equality with primitive values, but it's not the right way to do it with objects and I'll show you why. If those two values match, I'll output they match, and I'll add an else clause here, and if I get back false then I'll say…

Contents