Interview Questions & Answers

java

Since String is immutable, its hashcode is cached at the time of creation and it doesn’t need to be calculated again. This makes it a great candidate for key in a Map and it’s processing is fast than other HashMap key objects. This is why String is mostly used Object as HashMap keys.

The method intern() creates an exact copy of a String object in the heap memory and stores it in the String constant pool.

Note that, if another String with the same contents exists in the String constant pool, then a new object won’t be created and the new reference will point to the other String.

We can call the intern() method to tell the JVM to add it to the string pool if it doesn’t already exist there, and return a reference of that interned string:

Example:-

public class Main {
    public static void main(String[] args)
    {
        String s1 = "abc";
        String s2 = new String("abc");
        String s3 = new String("foo");
        String s4 = s1.intern();
        String s5 = s2.intern();

        System.out.println(s3 == s4);
        System.out.println(s1 == s5);
    }
} 
output:
false
true

Say, equals() method because it compares two string objects based on their content. That provides more logical comparison of two string objects. If you use “==” operator, it checks only references of two objects are equal or not. It may not be suitable in all situations. So, rather stick to equals() method to compare two string objects.

  1. A StringBuilder is not a thread-safe, whereas StringBuffer is a thread-safe
  2. A StringBuffer is slower than StringBuilder. Because StringBuffer is a thread-safe implementation and therefore slower than the StringBuilder.

  1. String class is immutable and whereas StringBuffer class is mutable.
  2. String is slow and consumes more memory when you perform too many concatenation String operations because every time it creates new instance. But StringBuffer is fast and consumes less memory when you concat strings.
  3. String class overrides the equals() method of Object class. So you can compare the contents of two strings by the equals() method. StringBuffer class doesn’t override the equals() method of Object class.
  4. String class uses String constant pool to store the objects whereas StringBuffer class uses heap memory to store its objects.

  1. A String is immutable in Java, while a StringBuilder is mutable in Java
  2. A String is thread-safe, whereas StringBuilder is not a thread-safe
  3. The performance of the String is slower than StringBuilder in the case of multiple concatenation operations. This is because the string is immutable in Java, and concatenation of two string objects involves creating a new object
  4. String class overrides the equals() method of Object class. So you can compare the contents of two strings by the equals() method. StringBuilder class doesn’t override the equals() method of Object class.

In general, both equals() and “==” operator in Java are used to compare objects to check equality but here are some of the differences between the two:
  1. The main difference between the equals() method and == operator is that one is a method and the other is an operator.
  2. We can use == operators for reference comparison (address comparison) and equals() method for content comparison. In simple words, == checks if both objects point to the same memory location whereas equals() evaluates to the comparison of values in the objects.

Example:-

public class Main {
    public static void main(String[] args)
    {
        String s1 = new String("HELLO");
        String s2 = new String("HELLO");
        System.out.println(s1 == s2);
        System.out.println(s1.equals(s2));
    }
} 
output:
false
true

                        

Yes, Strings are immutable in Java so once we create a String object then we can’t change its content. Hence it’s thread-safe and can be safely used in a multi-threaded environment.