top of page

Java String Interview Question

Writer's picture: Foramben PatelForamben Patel

1. What is a Java String, and how is it different from other data types?

A Java String is a sequence of characters used to represent text. Unlike primitive data types, such as int or double, which store single values, a String is an object that can store and manipulate a sequence of characters.


2. How do you create a String object in Java?

You can create a String object in Java by either using string literals (e.g., "Hello, World!") or by using the new keyword to instantiate a String object (e.g., String str = new String("Hello, World!");).


3. What is a String Pool in Java?

The String Pool is a memory optimization technique in Java that reuses string literals, reducing memory consumption when the same string value is created multiple times.


4. What is the basic difference between a String and String Buffer object?

String is an immutable object. Its value cannot change after creation. String Buffer is a mutable object. We can keep appending or modifying the contents of a String Buffer in Java.


5. What is the difference between String and String Builder/String Buffer classes?

  • String class represents a sequence of characters and provides useful methods to work with characters. String class instances are immutable. So each time we perform string concatenation using string class, a new object will be created with the concatenated string.

  • String Builder class is used to perform string concatenation operations in a more memory-efficient way. It internally maintains char[] and manipulates the content in this array only. When we need to get the complete concatenated string after performing all operations, it created a new string with the stored character array.

  • String Buffer is very much same as String Builder class. The only difference is that it is thread-safe. It's all methods are synchronized.


6. How to Compare Two Strings?

Another favorite area in interviews. There are generally two ways to compare objects

  • Using == operator

  • Using equals() method

The == operator compare for object references. So if two string objects are referring to the same literal in the string pool or the same string object in the heap then ‘s == t‘ will return true, else false.


The equals() method is overridden in String class and verifies the char sequences held by String objects. In other words, equals() method compares the values of string objects. If they store the same char sequence, the ;s.equals(t); will return true, else false.


7. Explain the concept of String immutability in Java. Why are strings immutable?

String immutability means that the content of a String cannot be changed after it is created. This design choice ensures that once a String is created, it remains unchanged, which is important for security and thread safety.


8. What is the difference between substring() and sub Sequence() methods in Java strings?

Both methods return a portion of the original string, but substring() returns a new String object, while sub Sequence() returns a Char Sequence, which may not necessarily be a String.


9. How can you convert a string to uppercase and lowercase in Java?

You can use the to Upper Case() and to Lower Case() methods on a String object to convert it to uppercase or lowercase, respectively.


10. Are Strings Thread-safe?

Yes, strings are thread-safe because they are immutable.

Remember that all immutable instances are thread-safe, by design.


11. What is the significance of the equals() method when comparing two strings?

The equals() method is used to compare the content of two strings for equality. It checks if the characters in both strings are the same. It's the recommended way to compare strings for equality.


12. What is the difference between == and .equals() when comparing strings in Java?

The == operator compares the references of two string objects to check if they refer to the same memory location. The .equals() method compares the actual content of the strings to check if they are equal. In most cases, you should use .equals() to compare strings for equality.


13. What is the use of to String() method in java ?

In Java, Object class has toString() method. This method can be used to return the String representation of an Object. When we print an object, Java implicitly calls toString() method. Java provides a default implementation for toString() method. But we can override this method to return the format that we want to print.


14. What is String interning?

String interning refers to the concept of using only one copy of a distinct String value that is Immutable. It provides the advantage of making String processing efficient in Time as well as Space complexity. But it introduces extra time in creation of String.


15. Why Java uses String literal concept?

Java uses String literal concept to make Java more efficient in memory. If same String already exists in String constant pool, it can be reused. This saves memory usage.


16. What is the substring() method used for in Java strings?

The substring() method is used to extract a portion of a string. It takes one or two arguments, specifying the start and optionally the end index of the substring.


17. What is the compare To() method in Java strings used for?

The compare To() method is used to compare two strings lexicographically (i.e., based on their character order). It returns a negative integer, zero, or a positive integer depending on whether the first string is less than, equal to, or greater than the second string.



 
 
 

Recent Posts

See All

Comments


bottom of page