String 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
123456789package 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
1234567891011package 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
1234567891011package 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.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 2.7
String 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
123456789package 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
1234567891011package 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
1234567891011package 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.
Thanks for your feedback!