Top 20 Java String Interview Questions and Answers

This article provides 20+ Java String Interview Questions and Answers.
String related interview questions come up frequently in Java interviews. So, we hope these help you prepare well for the same.

Java String Interview Questions

Java String Interview questions & answers:

  1. What is String in Java?
  2.  

  3. What are different ways to create Strings?
  4.  

  5. What is String constant pool?
  6.  

  7. What is the difference between following statements :

    i) String str = “abc”;
    ii) String str = new String(“abc);

  8.  

  9. How to compare two Strings in java program?
    (or)
    What is the difference between comparing Strings with == operator and equals() method ?
  10.  

  11. How to convert String to character array and vice versa?
  12.  

  13. Can we use String in Switch expression?
  14.  

  15. What is the difference between String, StringBuffer and StringBuilder classes ?
  16.  

  17. What is String immutability and why is String immutable in Java ?
  18.  

  19. How to Split String in java?
  20.  

  21. Why is charcter array preferred over String for storing password?
  22.  

  23. What does String intern() method do?
  24.  

  25. Are Strings thread-safe in Java?
  26.  

  27. Why String is mostly used as key in HashMap?
  28.  

  29. What are different String concatenation methods in Java ?
  30.  

  31. What are different approaches to split a String in Java ?
  32.  

  33. How to convert an InputStream to a String ?
  34.  

  35. How to convert a String to int in Java ?
  36.  

  37. How to convert String to LocalDate, LocalDateTime in Java ?
  38.  

  39. How to convert java.util.stream.Stream to String ?

 
If you are looking for Frequently asked Coding Interview Questions on Strings, refer this post :

20 Java String Coding Interview Questions
 

What is String in Java?

In Java, String is a class defined the package java.lang.

A String represents a group of characters.

Strings are constant in Java; their values cannot be changed after they are created.
 

What are different ways to create Strings?

We can create Strings in following ways :

1) We can assign characters to a String variable

String str = "abc";

2) String(String str) constructor
String str = new String("abc");

3) Using the String(char[] value) and String(char[] value, int offset, int count) constructors

char[] chrArray = {'a','b','c'}
String str = new String(chrArray); 
System.out.println(str); // prints abc

String str2 = new String(chrArray,1,2);
System.out.println(str2); // prints bc

Refer this article for more details :

Strings in Java

 

What is a String constant pool ?

A String constant pool is a separate block of memory that JVM maintains for storing String objects.

If a String is created using the assignment operator directly(not using new operator), JVM stores it in String constant pool.

String str = "abc"; // will be stored in String constant pool

String str = new String("abc"); // will not be stored in String constant pool

 

What is the difference between following statements :

1 ) String str = “abc”;

2) String str = new String(“abc);

In statement 1, a string literal is assigned to String variable str. Here, the JVM checks for a matching object in String constant pool. If it is not available, it creates a new String object and stores in the pool. If the object is found, it simply creates a reference to it, but doesn’t create a new object.

In statement 2, JVM always creates a new object without refering the String constant pool.

 

How to compare Strings in java program?

Here are some ways to compare Strings in Java.

1) == operator : Compares references of the obejcts, not the contents)
2) equals() : Compares the contents of the Strings
3) equalsIgnoreCase() : Compares two Strings irrespective of their case
4) compareTo() & compareToIgnoreCase() : Compares two strings lexicographically
5) contains() : check is a String contains specified sequence of characters

There are also methods like regionMatches(), startsWith() and endsWith() that are used for comapring Strings.

Refer this post to read details on Different Methods of String Comparison in Java.

 

How to convert String to character array and vice versa?

String has a toCharArray() method to convert String to character array.

String str = "hello";
char[] tgt = str.toCharArray();

The constructor String(char[] value) can be used to convert a character array to a String.

char[] chrArray = {'a','b','c'}
String str = new String(chrArray); 

 

Can we use String in switch expression?

JDK 7 onwards, Strings can be used in switch statements as the expression.

Refer this post for more details on this :

String in switch statement in Java 7

 

What is the difference between String, StringBuffer and StringBuilder classes ?

Strings are immutable in Java. StringBuffer provides a mutable alternative to String.

StringBuffer operations synchronized and hence provides thread safety.

StringBuilder is another mutable alternative to String. However, it is not synchronized.

StringBuffer should be used in multithreaded evironment to avoid data corruption. StringBuilder is faster compared to StringBuffer as there is no overhead for synchronization.

 

What is String immutability and why is String immutable in Java ?

Refer the following article :

What is String immutability and why is String immutable in Java

 

How to Split String in java?

We can use split(String regex) to split the String into String array based on the provided regular expression.
Learn more in this article : Split String in Java.

 

Why Char array is preferred over String for storing password?

String is immutable in java and stored in String pool. Once it’s created it stays in the pool until unless garbage collected, so even though we are done with password it’s available in memory for longer duration and there is no way to avoid it. It’s a security risk because anyone having access to memory dump can find the password as clear text.
If we use char array to store password, we can set it to blank once we are done with it. So we can control for how long it’s available in memory that avoids the security threat with String.

 

What does String intern() method do?

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
This method always return a String that has the same contents as this string, but is guaranteed to be from a pool of unique strings.

 

Are Strings thread-safe in Java?

String are thread safe because of their immutability.

 

Why String is mostly used as key in HashMap?

Since Strings are immutable, they can be cached and perform better compared to mutable counterparts. This is the reason why Strings are preferred as keys in hashmaps.

In a HashMap, being immutable guarantees that hashcode will always the same, so that it can be cached without worrying the changes.

 

Different String concatenation methods in Java

Java provides following ways to concatenate strings :

1) Using + operator

2) Using String concat() method

3) Using StringBuffer/StringBuilder append() method

4) Using String join() method (added in JDK 8)

Read this article for more information on these methods :

Different Methods of String concatenation in Java

 

What are different approaches to split a String in Java ?

Here are some approaches to split a String in Java.

– Using String split() method
– Using StringTokenizer

StringTokenizer is a legacy class retained for legacy reasons although its use is discouraged in new code.

It is recommended to use the String split method or the java.util.regex package for the same functionality.

Refer this post for more details on these :

Split string in Java

 

How to convert an InputStream to a String ?

Scanner s = new Scanner(inputStream).useDelimiter("\\A");
String result = s.hasNext() ? s.next() : "";

 

How to convert a String to int in Java ?

Different ways to convert the string representation of an integer into an integer in java
– Using parseInt()
– Using valueOf()

Refer following article for details :

How to convert a String to int in Java

 

Convert String to LocalDate, LocalDateTime in Java

The Java 8 LocalDate-Time API includes a parse() method, which can be used to parse a given input string using a specified format.

Refer :

Java 8 – Convert String to LocalDate, LocalDateTime in Java

 

How to convert java.util.stream.Stream to String ?

Collectors joining() method can be used along with Stream collect() operation to convert Stream to a String.

Refer :

Convert java.util.stream.Stream to String using Collectors.joining()
 

String Programming Questions

Here are 20 Frequently asked Java Programming Interview questions on Strings :

Java Programming Interview questions on Strings
 

© 2017 – 2018, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags