String interview questions in java

Here we will see String interview questions with examples.

  1. What is String in java? Points related to String.
  2. String constant pool in Java.
  3. Comparing of two String using == and equals().
  4. How will we define an immutable class in java?
  5. How does compareTo() method work in java?
  6. Why is String immutable in java?
  7. Important methods of String.
  8. Program related to String which frequently asked in the interview.
  9. Difference between String and StringBuffer in java.
  10. String to int in java.

 

What is String in java? Points related to String.

  1. String is a final class which is available in java.lang package. Since String is a final class, no other class can extend it.
  2. String Objects are immutable in nature.
  3. String implements Serializable,Comparable and CharSequence interface.
  4. String overrides equals() and hashCode() methods.
  5. String works on String constant pool.

Let’s see all points in details –

String is a final class which is available in java.lang package. Since String is a final class, no other class can extend it.

If you define your own class and try to extend String, it will give the compilation error. Let’s see an example –

 

String Objects are immutable in nature – This one is a very important point for interview perspective. What does it mean when we say String is immutable. Immutability is – Once we assign some value to String, that value can never be changed.

String implements the Serializable, Comparable and CharSequence interface – String implements Serializable, Comparable and CharSequence interfaces. We override compareTo(Object object) method in String class and define custom logic. Let’s see how it is implemented –

String overrides equals() and hashCode() methods – This is another important point related to String, which makes String differ from StringBuffer. In String equals() and hashcode() is overridden and it has been defined custom logic inside equals() and hashcode(). Let’s see code snippet, how equals() and hashcode() is overridden.

   //hashCode() implementation inside String class
   public int hashCode() {
        int h = hash;
        if (h == 0 && value.length > 0) {
            char val[] = value;

            for (int i = 0; i < value.length; i++) {
                h = 31 * h + val[i];
            }
            hash = h;
        }
        return h;
    }

// equals() method implementation inside String class

 public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) {
                    if (v1[i] != v2[i])
                        return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }

 

String works on String constant pool – String uses special memory location for reusability of String literals that is called String constant pool.