JAVA · ОСНОВЫ · LESSON 22

Compare the words

String.equals · about 14 minutes

Learning with progressRead the summary for free. To save the work, log in to the account.
STEP 1

Words are compared by method

Use equals to check for the same words. It answers true or false. It is better not to take two signs == for this beginner.

String answer = "да";
System.out.println(answer.equals("да"));

✦ The result is true.

STEP 2

Verification under condition

The answer to equals can be put inside if. Then the program will choose the action by word.

if (answer.equals("да")) {
    System.out.println("Понял!");
}
Verification of words
answeryesequalscomparetruecoincidentally

✦ It works if the words are exactly the same.

IN-CLASS PRACTICE

Type the code yourself

Type an example manually. Check the signs and brackets.

public class Main {
    public static void main(String[] args) {
        String answer = "да";
        System.out.println(answer.equals("да"));
    }
}
Open the editor for class work
HOMEWORK

Solve it yourself

Create String word = «кот». Using equals, compare word with the word «пёс» and derive the result.

The answer is hidden. Use similar examples from the lesson notes.

Opening up your homework