Frequently asked Java String interview questions and answers
String is the most important class in Java. There can be hardly any Java interview without a question about String. String objects are treated as special in Java which makes it an interesting candidate for Java interviews. In this post we will see some of the frequently asked Java String interview questions and answers for freshers and also for experienced programmers. Knowing answers to these questions will be very helpful to tackle any questions asked related to String in interview.
1. What is String in Java? String is a primitive or object(derived) data type?
Understanding what a String is very important to understand anything else related to String. String is a Class in java and defined in java.lang package. It is not a primitive data type like int, long or double. The String class represents a sequence of characters. All string literals in Java programs, such as “abc”, are implemented as instances of the String class.
String is immutable and final in Java and JVM uses String Constant Pool to store all the String objects.
2. Why String is final in Java or why string objects are immutable?
This is one of the most important questions related to String. To answer this, first you need to know what is immutability. Immutable simply means that once string objects are created(in the heap memory) its contents cannot be changed or modified. For example if we say,
String s = new String("Hello");
The output of the above code will be “Hello”. This is because even though you have called toUpperCase() on the String object, its contents will not be changed, rather a new string object will be created in the heap memory with all upper case letters(HELLO) and the original string object remains unchanged. This is the concept of immutability.
String objects are immutable or final in Java because String literals are stored in String Constant pool and shared between multiple applications. Hence for performance and security reasons, String is made immutable and final in Java. Also Immutable strings are thread safe. So, we can use them in a multi threaded code without synchronization. You can read more on this link.
3. In how many ways we can create the string object?
There are two ways to create string objects in java. One is using new operator and another one is using string literals. There are several constructors available in String class to get String from char array, byte array, StringBuffer and StringBuilder.
String s1 = new String("abc");
String s2 = "abc";
When we create a string using double quotes, JVM looks in the String constant pool to find if any other String is stored with same value. If found, it just returns the reference to that String object else a new String object with given value is created in the heap memory and stores a reference to it in the String constant pool.
When we use new operator, JVM creates a new string object irrespective of whether there is already a reference in the String constant pool for a string with same value.
4. How many objects will be created in the following code ?
String s1 = new String(“abc”); //1
String s2 = “abc”; //2
Two string objects will be created by the above code. The first line creates a new string object in the heap memory. The second line also creates a new string object in the heap memory and a reference to it will be added to the String constant pool.
5. How many objects will be created in the following code ?
String s1 = “abc”; //1
String s2 = “abc”; //2
Only one string object will be created. The first line creates a new string object in the heap memory and a reference to it will be added to the String constant pool. In the second line, the same reference stored in the string constant pool will be assigned to s2.
6. What is the use of intern() method in String?
A pool of strings, initially empty, is maintained privately by the class String.
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.
It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.
String s1 = new String("abc"); // 1
String s2 = "abc";
System.out.println(s1 == s2); // prints false
s1 = s1.intern();
System.out.println(s1 == s2); // prints true
7. Are strings thread-safe in Java?
Since String objects are immutable, it is thread-safe and it can be shared between multiple threads without external synchronization.
8. What is String constant pool or String literal pool in Java?
Read my another post for a detailed answer.
9. How do you create mutable string objects?
10. What is the difference between String, StringBuffer and StringBuilder?
- You have to use new operator to create objects of StringBuffer and StringBuilder classes. You can’t use string literals to create objects of these classes. For example, you can’t write
StringBuffer sb = “abc” or StringBuilder sb = “abc”. It gives compile time error. But, you can use both string literals and new operator to create objects of String class.
- String and StringBuffer objects are thread safe whereas StringBuilder objects are not thread safe.
- Because of thread safety, String and StringBuffer objects are performance wise slower than StringBuilder objects.
- equals() and hashCode() methods are overridden in String class whereas in StringBuffer and StringBuilder classes it is not overridden.
11. How do you compare two Strings in Java?
equals() or equalsIgnoreCase() method should be used to compare two string objects in Java. You should not use equality operator “==” for comparing two string. The “==” operator checks only references of two objects. Hence it may work in some case and in some cases it won’t.
12. What is the main difference between Java strings and C, C++ strings?
In C and C++, strings are terminated with a null character whereas in Java it is not terminated with null character. Strings are treated as objects in Java whereas in C & C++ it is considered as a character array.
13. Can we use String in switch case?
This is a tricky question used to check your knowledge of latest happenings in Java. Java 7 extended the capability of switch case to use Strings also, earlier java versions doesn’t support this.
If you are implementing conditional flow for Strings, you can use switch case if you are using Java 7 or higher versions.
14. Write a Java program to check if input String is Palindrome?
15. Write a Java program to reverse a String without using reverse() function.
16. Write a Java program to count the occurrences of a character in string.
17. Write a Java program to find the length of a String without using inbuilt functions.
18. Write a Java program to check if given strings are Isomorphic.
19. Write a Java program to find first non repeated character in a string.
20. Write a Java program to check if two Strings are anagram in Java.
That’s all about frequently asked Java String interview questions and answers for freshers and also for experienced developers.
If any other important question is missing, add it in the comments section.
Latest posts by Uday (see all)
- Introduction to Java Enum data type with example programs - March 31, 2017
- How to send mail using JavaMail API - March 1, 2017
- WatchService in Java with example program - February 27, 2017