Top 20 Java String Interview Questions

Questions and Answers

This article will be very helpful for freshers to get complete knowledge of Java String and tackle any questions asked related to String in Java interview.

The following are top 20 frequently asked questions about Java Strings.

1. What is String In Java?

* String is a Class in java and defined in java.lang package.

* It is not a primitive data type like int and long. String class represents character Strings.

* String is used in almost all the Java applications.

2. Is String is a primitive or derived data type?

String is a derived data type.

3. Is String objects are immutable?

Yes String object is immutable i.e once we create a String object the content of that string cannot be modified.

Example

String s = new String(“Beyondcorner”);

s.toUpperCase ();

System.out.println (s);

Explanation

Output = “Beyondcorner”.

In this example we have called toUpperCase() on the String object, its contents will not be changed, but new string object will be created in the heap memory with all upper case letters (BEYONDCORNER) and the original string object remains unchanged.

4. What are the different ways to create String object?

There are two ways to create string objects in java.

1. Using new operator

* The objects created using new operator are stored in the heap memory.

Example

String s1 = new String(“beyond”)

2. Using string literals

* The objects created using string literals are stored in string constant pool.

Example

String s2 = “beyond”

5. How to convert String to char and Vice versa?

It is a tricky question because String is a sequence of characters, so we can’t convert it to a single character. we can use below two methods like,

1. CharAt – It will get the character at given index.

2. toChatArray() – It will convert String to character array.

Example

String to char Java program

6. How to Split String in Java?

Using split (String regex) method we can split String in java.

Example

String s = abcaaba”;

System.out.println(Arrays.toString(s.split(“a”)));

Output: [, bc, , b]

Explanation

In the above example it split the string based on the given regular expression string i.e “a”. The whole string is split and returned in the form of string array.

7. What is String pool?

String Pool is a pool of Strings stored in java heap memory.

We all know that String is special class in java and we can create String object using new operator and also providing values in double quotes.

8. Why string is popular in java hash map?

It is popular because of following reasons,

* String is immutable in nature.

* It’s processing speed is fast i.e string hash code is cached at the time of creation and it doesn’t need to be calculated again.

9. How to Create mutable String objects?

The mutable objects can be created using,

  1. StringBuffer classes
  2. StringBuilder classes

10. How do you Compare two Strings in java?

The below two methods are used to Compare the Strings in java,

1. equals() method

2. equalsIgnoreCase() method

Note: We should not use equality operator  “==” for comparing two string. 

11. What is the main difference between Java Strings and C, C++ Strings?

Java StringsC,C++ Strings
It is not terminated with null character in java.Here strings are terminated with a null character.
Strings are treated as objects in java.It is considered as a character array.

12. How to Repeat string in java?

In Java, we can use the repeat() method of StringUtils from Apache Commons Lang package.

Example

String str = “beyond”

String repeated = StringUtils.repeat (str,3)

Output: beyondbeyondbeyond

13. How to Optimize Java string creation?

By using string literals we can optimize java strings.

Example

Optimize Java string creation program

14. Write java program to remove particular character from a string?

Example

remove particular character from a string program

15. Which are the final class among these three classes – String, StringBuffer, and StringBuilder?

All three classes are final class.

16. Are strings thread-safe in Java?

yes String thread safe in java, because

  • String objects are immutable in nature.
  • It can be shared between multiple threads without external synchronization.

17. How to convert string to int in java?

The Integer.parseInt() method is used to convert string to int.

Example

int n = Integer.parseInt(“100”);

18. How to convert String to date?

Example

String str = “May 16, 2018”;

date date = new SimpleDateFormat (“MMMM d, YY”, Locale.ENGLISH) .parse(str);

System.out.println(date);

Output:

Wed May 16 00:00:00 EDT 2018

19. Which operator is overloaded for String?

The ‘+’ is overloaded operator in Java for String.

* It is used for concatenating two strings.

20. How many objects will be created If two Strings are Created This way

String S1 = “beyond_corner”;
String S2 =  “beyond_corner”;

only one object is created, because s1 and s2 are string literals and having the same content object reference will be shared by them in the string pool.

 

One comment

Leave a Reply

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