Comparing String Objects, intern() & other confusions

We all know that we use .equals() to compare two strings & we do not use == to compare them. The primary reason for that is "==" compares the physical location of the Strings(i.e. addresses) & not the value in them.
But, we should know what will happen if we compare two strings using "==" for interviews or may be just for fun!!
Let's follow the below piece of code -
ex-1
public class compareStrings {
 public static void main(String[] args) {
  String a = new String("Test");
  String b = new String("Test");
  // comparing the Strings as objects & not the value
  System.out.println(a==b);
 }
}

The result is : false
It is obvious. Because we are creating two different objects of String class & comparing them. Obviously their addresses are different.
Now, let's look at the below code:











ex-2
public class compareStrings {
 public static void main(String[] args) {
  String a = "Test";
  String b = new String("Test");
  // comparing the Strings as objects & not the value
  System.out.println(a==b);
 }
}

Again, the result is false & the explanation is same.
Now, here is the another example
ex-3
public class compareStrings {
 public static void main(String[] args) {
  String a = "Test";
  String b = "Test";
  // comparing the Strings as objects & not the value
  System.out.println(a==b);
 }
}
The Result is true
Now, you may ask WHY ?

The answer is : there is a concept called "String Constant Pool". Whenever we create a String object using String literals (String a = "Test") Java records an entry in String Constant Pool. And, at the same time, when we create an object of String using literals, first it searches in the String Constant pool if the String already exists there. If it is not there, it creates a new object & record it in the String Constant Pool.

But when we create String object using "new String()", it doesn't do any kind of checking & create an object of String instantly. That's why in example 2, the result if false though the String was already available in the String constant pool.
So, as we know that Strings are immutable(To know what is immutable is, check this link - Strings are immutable), it is a good practice to create String objects using literals to optimize the memory usage of JVM.

So, what is intern() method?
By using intern() method, we can add a String (which is created by new String() method) to String constant pool or while creating a String(using new String()) we can made it check String Constant Pool before creating a new instance.
Here goes the example:
public class InternString {

 public static void main(String[] args) {
  
  // String f checks the String constant pool before creating a new instance.
  // it finds the existing String e.
  String e = "Test";
  String f = new String("Test").intern();
  // comparing the Strings as objects & not the value
  System.out.println(e==f);
  
  
  //String h checks the String Constant pool before creating instance
  // no such String found in the pool
  // creates a new Object & records it in String Constant Pool
  String g = new String("Test1").intern();
  String h = "Test1";
  // comparing the Strings as objects & not the value
  System.out.println(g==h);
 }

}

The result is

true 
true

Happy learning !!

No comments :

Post a Comment