Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
String Pool, Method Equals() | String
Java Basics
course content

Course Content

Java Basics

Java Basics

1. Getting Started
2. Basic Types, Operations
3. Loops
4. Arrays
5. String

bookString Pool, Method Equals()

The equals() Method

This chapter provides a brief overview of the equals() method. This method is used to compare objects and is commonly used with String objects. Let's examine an example of creating two identical string variables and comparing them using "==":

java

Main

copy
123456789
package com.example; public class Main { public static void main(String[] args) { String first = new String("string"); String second = new String("string"); System.out.println(first == second); } }

In the response, we get "false," but why? The values of the two string variables are the same and equal to "string." It is because "==" compares the references to objects in memory, not the actual values. Our objects are outside the String pool.

String Pool

What is the String Pool? It is an area in the heap memory that is allocated when creating string objects. If we create a String object with the value "string" and then create another String object with the same value, both objects will refer to the same object in memory, which is located in the string pool.

So why did we get "false" in our case? It is because we bypassed the string pool by creating objects using the syntax String first = new String("string");. When we create a String object using a different syntax, such as String first = "string";, it will be placed in the string pool.

Let's consider an example of code:

java

Main

copy
1234567891011
package com.example; public class Main { public static void main(String[] args) { String first = "string"; String second = "string"; String third = new String("string"); System.out.println("Result of comparing first and second: " + (first == second)); System.out.println("Result of comparing first and third: " + (first == third)); } }

Let's consider a diagram explaining how it works and which objects are in the String Pool.

How do we compare the values of strings in the String Pool and those outside it? For this purpose, Java provides the equals method, which compares the values of our String objects instead of their references. Let's consider an example code where we compare strings using the equals method instead of ==.

java

Main

copy
1234567891011
package com.example; public class Main { public static void main(String[] args) { String first = "string"; String second = "string"; String third = new String("string"); System.out.println("Result of comparing first and second: " + (first.equals(second))); System.out.println("Result of comparing first and third: " + (first.equals(third))); } }

Now we can see that we have the correct comparison.

Note

Use the equals() method instead of == when working with String.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 5. Chapter 7
some-alt