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

bookString Pool, Method Equals()

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 ==:

Main.java

Main.java

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); } }

The result is false because == compares object references, not their actual values. Even though both strings contain "string", they are different objects in memory and are created outside the String pool.

String pool

The String Pool is a part of heap memory where string literals are stored. When you create strings with the same value using literals, they reference the same object in memory.

So why did we get false? Because we bypassed the string pool by using new String("string"), which creates a new object in memory. When using String first = "string";, the string is placed in the string pool and can be shared.

Let’s look at a code example:

Main.java

Main.java

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

Main.java

Main.java

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.

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 7

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookString Pool, Method Equals()

Swipe to show menu

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 ==:

Main.java

Main.java

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); } }

The result is false because == compares object references, not their actual values. Even though both strings contain "string", they are different objects in memory and are created outside the String pool.

String pool

The String Pool is a part of heap memory where string literals are stored. When you create strings with the same value using literals, they reference the same object in memory.

So why did we get false? Because we bypassed the string pool by using new String("string"), which creates a new object in memory. When using String first = "string";, the string is placed in the string pool and can be shared.

Let’s look at a code example:

Main.java

Main.java

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

Main.java

Main.java

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.

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 7
some-alt