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

Course Content

Java Basics

String Pool, Method Equals() String 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.java

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.java

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.java

Now we can see that we have the correct comparison.

Note

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

Everything was clear?

Section 5. Chapter 7
course content

Course Content

Java Basics

String Pool, Method Equals() String 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.java

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.java

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.java

Now we can see that we have the correct comparison.

Note

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

Everything was clear?

Section 5. Chapter 7
some-alt