Skip to content

Few tricky Java Strings interview questions

#Post3

Q. 1 Use of String.intern(), will it release memory from Heap?

The String.intern() method is used to optimize memory usage when working with a large number of String objects. When a new String is created using the new keyword, a new object is created in memory.

However, if that string already exists in the string pool then the intern() method can be used to return a reference to the existing string in the pool instead of allocating a new object.

Q. 2.public static void main(String args…) {}, Will it work as main method

Yes, above code will work. As var args internally converted to array of a passed parameter, here in this case its String, internally it will be created as String[]

Q. 3. Why is the string immutable

The immutability of Strings in Java is a design decision that has a number of benefits in terms of security, thread-safety, caching and optimization.

Why other classes are not immutable in java?

While Strings are immutable in Java, other classes are not necessarily immutable. In fact, most classes in Java are mutable by default. There are several reasons why other classes are not immutable:

  1. Flexibility: Mutable objects are more flexible than immutable objects because they can be modified after they are created. This can be useful in situations where the object needs to be updated frequently, or when the object needs to be used as a shared resource across multiple threads.
  2. Performance: In some cases, mutable objects can be more efficient than immutable objects because they can be modified in place, without the need for creating new objects. This can be particularly important in performance-critical applications.
  3. Implementation complexity: Implementing a class as immutable can be more complex than implementing it as mutable.
Q. 4. String a = “yes” String b = “yes”
a==b or a.equals(b) what is the result?

In the above example a==b is true And a.equals(b) as well true, because we have created stringa using literals which will be stored on string constant pool. So a==b will point to same memory reference locations, hence it is true.

Q. 5.Why is password stored using char[] and not in String?

When a password is stored in a String object, it remains in the memory until the garbage collected. This could lead to a security vulnerability, as an attacker could potentially access the password from the memory before it is garbage collected.

In case of char[], it can be overwritten or cleared as soon as it is no longer needed. This way, the password does not remain in the memory for an extended period and cannot be easily retrieved by an attacker.

Also if we do toString() to it, it will just returns hash object id and not content.

But don’t forgot to clear them after use.

Q. 6. String s = new String (“Hello”)
How many objects are getting created here?

When the statement String s = new String(“hello”) is executed, two String objects are created in memory.

The first String object is created using the string literal “Hello”. This String object is automatically interned by the compiler, so if “Hello” has not already been interned in the string pool, a new String object will be created in the pool to represent it.

The second String object is created explicitly using the new keyword. This creates a new String object on the heap.

Q.7. StringJoiner class?

The String Joiner class is a utility class in Java that provides a convenient way to join multiple strings with a delimiter. It was introduced in Java 8 and is part of the java.util package.

StringJoiner joiner = new StringJoiner(", ", "[", "]");
joiner.add("apple");
joiner.add("banana");
joiner.add("orange");
String joinedString = joiner.toString();
System.out.println(joinedString); 
// prints "[apple, banana, orange]"

Leave a Reply

Your email address will not be published. Required fields are marked *

Top Time Complexities Top Java String Tricky interview questions 7 steps to build programming logic