CodeGym /Java Course /Module 1. Java Syntax /Comparing strings by content

Comparing strings by content

Module 1. Java Syntax
Level 4 , Lesson 6
Available

1. Comparing strings

This is all well and good. But you can see that the s1 and s2 strings are actually the same, meaning that they contain the same text. When comparing strings, how do you tell the program to look not at the addresses of String objects, but at their content?

To help us with this, Java's String class has the equals method. Calling it looks like this:

string1.equals(string2)
Comparing two strings

This method returns true if the strings are the same, and false if they are not the same.

Example:

Code Note
String s1 = "Hello";
String s2 = "HELLO";
String s3 = s1.toUpperCase();

System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s2.equals(s3));
// Hello
// HELLO
// HELLO

false // They are different
false // They are different
true // They are the same, even though the addresses are different

More examples:

Code Explanation
"Hello".equals("HELLO")
false
String s = "Hello";
"Hello".equals(s);
true
String s = "Hel";
"Hello".equals(s + "lo");
true
String s = "H";
(s + "ello").equals(s + "ello");
true

4
Task
New Java Syntax, level 4, lesson 6
Locked
Even numbers
Use a for loop to display even numbers from 1 to 100 inclusive. Display each value on a new line.
4
Task
New Java Syntax, level 4, lesson 6
Locked
Drawing a rectangle
Use the keyboard to enter two numbers m and n. Using a for loop, display an n x m rectangle made of eights. Here's an example: m=2, n=4 8888 8888

2. Case-insensitive string comparison

In the last example, you saw that the comparision "Hello".equals("HELLO") yields false. Indeed, the strings are not equal. But...

Clearly, the strings are not equal. That said, their content has the same letters and only differs by the case of the letters. Is there any way to compare them and disregard the case of the letters? That is, so that "Hello".equals("HELLO") yields true?

And the answer to this question is yes. In Java, the String type has another special method: equalsIgnoreCase. Calling it looks like this:

string1.equalsIgnoreCase(string2)

The name of the method translates roughly as compare but ignore case. The letters in the method's name include two vertical lines: the first is a lowercase L, and the second is an uppercase i. Don't let that confuse you.

Example:

Code Note
String s1 = "Hello";
String s2 = "HELLO";
String s3 = s1.toUpperCase();

System.out.println(s1.equalsIgnoreCase(s2));
System.out.println(s1.equalsIgnoreCase(s3));
System.out.println(s2.equalsIgnoreCase(s3));  
// Hello
// HELLO
// HELLO

true
true
true

4
Task
New Java Syntax, level 4, lesson 6
Locked
Triangle of eights
Using a for loop, display a right triangle of eights, with a base of 10 and a height of 10. Example output: 8 88 888 8888 88888 888888 8888888 88888888 888888888 8888888888

3. Example of string comparison

Let's give just one simple example: suppose you need to enter two lines from the keyboard and determine whether they are the same. This is what the code will look like:

Scanner console = new Scanner(System.in);
String a = console.nextLine();
String b = console.nextLine();
String result = a.equals(b) ? "Same" : "Different";
System.out.println(result);

4. An interesting nuance of string comparison

There is one important nuance that you need to be aware of.

If the Java compiler finds multiple identical strings in your code (specifically in your code), then it will create only a single object for them in order to save memory.

String text = "This is a very important message";
String message = "This is a very important message";

And here's what memory will contain as a result:

string comparison

And if you compare text == message here, then you get true. So don't be surprised by that.

If for some reason you really need the references to be different, then you can write this:

String text = "This is a very important message";
String message = new String ("This is a very important message");

Or this:

String text = "This is a very important message";
String message = new String (text);

In both of these cases, the text and message variables point to different objects that contain the same text.


4
Task
New Java Syntax, level 4, lesson 6
Locked
Drawing lines
Using a for loop to display: - a horizontal line of 10 eights - a vertical line of 10 eights (do not count any of the eights in the horizontal line as part of this vertical line).
4
Task
New Java Syntax, level 4, lesson 6
Locked
Chain letter
Enter a name from the keyboard and use a for loop to display the following 10 times: loves me. Example output for the name "Scarlett": Scarlett loves me. Scarlett loves me. Scarlett loves me. Scarlett loves me. Scarlett loves me. Scarlett loves me. Scarlett loves me. Scarlett loves me. Scarlett lo
Comments (7)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Kristina Pfeifer Level 4, Austria
23 February 2024
Unfortunately it did not let me do a ternary with a lazy output: System.out.println("The strings are " + out); Though, it worked!
Kevin Level 5, United States
19 February 2024
Very important lesson I didn't know.. that compiler tells me the single most reason to use 'new String'
Anonymous #11367710 Level 7, Bowie, United States
6 August 2023
Can we use a private helper method in Comparing Strings by Reference to avoid having to re-type the if conditions and print statements?
Aishwarya Level 4, deagu , South Korea
5 May 2023
What ia the uae of console in java?
Abhishek Tripathi Level 72, Rewa, India Expert
30 June 2023
It is nothing but the name of a variable having Scanner object.
Damian Braś Level 11, Poland
31 January 2023
Why this lesson is not active for me? I have 4 level and i don't know how going done task,, :P
Dillon Morgan Level 21, London, United Kingdom
9 October 2022
This lesson leads directly from the previous lesson where it says "This is all well and good". Slightly confusing when starting fresh on this lesson. Also, in Section 2 the line at the end of the first paragraph is almost repeated at the start of the second (E.g. "Indeed, the strings are not equal, but...").