Java String Interview Questions: String is the sequence of characters in Java. String implements the interfaces Serializable, CharSequence, Comparable<String> and extends the Object class in Java. Strings value cannot be changed. Strings are immutable in Java.
In Java, we can represent the sequence of characters in “3” different ways. They are:
1) Using the object of the String class.
2) Using the object of the String Buffer and String Builder class.
3) Using the object of an array of characters.
Here, we would like to collect the Java String Interview Questions and its FAQ’s. If you have any queries, comment down your questions.
Java String Interview Questions and Answers
1) What is String in Java? Is String a data type?
A) A string is a class in java.lang package, but in java all classes are considered as a data types. Hence, we can take String as a data type too.
2) Is String class final in Java?
A) Yes. String class is declared as final in Java.
3) What are the interfaces String class Implements?
A) String class implements “3” interfaces in Java. They are:
4) What is String Constant pool (String literal pool)?
A) String Constant pool is a separate block of memory where the string objects are held by JVM.
5) Explain the difference between the following statements?
String s = “Hello”;
String s = new String(“Hello”);
A) In first statement assignment operator is used to assign the string. In this case, JVM first checks the String pool whether the same value is already available in the String container or not, if available then it creates another reference to it, if not available then it creates a new one. In second case each and every time it creates a new object of string.
6) How to concat two different String?
A) We can concat two different Strings with the help of “+” assignment operator or concat() method of String class.
7) Explain about concatenation operator (+) in Java Strings?
A) The functionality of the concatenation operator is to create a new String class object with the data present at the left-hand side and right-hand side.
8) What do you mean by immutability?
A) Immutability means once an object is initialized we can’t change.
9) How to make a class immutable in java?
A) The following ways we can make a class immutable in java:
1) Make a class final or private.
2) Remove constructor.
3) Allow only getter method in your class.
10) What is String Buffer?
A) String Buffer is like a String but can be modified in the same memory location. Every method of the String Buffer class would be changing the data within the same object and returns the address of the same object.
11) What is String Builder?
A) String Builder is a mutable sequence of characters. String Builder class is designed for use as a drop-in replacement for StringBuffer in places where the String buffer was being used by a single thread.
12) What is the difference between String and StringBuffer classes?
|1) String class object is immutable hence there contain cannot be modified.||1) StringBuffer object is mutable so they can be modified.
|2) The method that directly manipulates data of the object in String class.||2) Such methods are available in String class.
|3) Append(),reverse() method not available in String class.||3) Append(),reverse() method available in StringBuffer class.|
13) What is the default capacity of String?
A) Strings do not have default capacity. The passed String length is its capacity.
14) What is the default capacity of String Buffer?
A) String Buffer has default capacity as 16 buffers. It increases its capacity automatically when sizes reach maximum using its capacity formula ( ( current capacity * 2 ) + 2).
15) In how many ways we can concat String in Java?
A) Using String object, we can concat new string to the current in two ways.
1) using + operator.
2) using concat() method.
16) What is the difference between equals() and “ == ” operator?
A) The equals() method compares the characters inside a String object. The “==” operator compares two object references to see whether they refer to the same instance.
17) In how many ways we can concat String Builder in Java?
A) Using String Buffer we can perform the concat operation only in one way.
1) using append() method.
18) What is the use of the charAt() method?
A) CharAt() method returns the character at the specified location.
String name = “TutorialsWeb”;
Char ch = name.charAt(7);
19) There are any other classes in java that is immutable?
A) All the wrapper classes in Java are immutable like Integer, Float, Double etc.
String Interview Questions and Answers in Java
20) What is String Tokenizer class?
A) String Tokenizer class is used to break the string in the form of tokens.
21) There is any alternative of string Tokenizer?
A) String class is alternative of string tokenizer, with the help of split() method of string.
22) What is the difference between StringBuffer and StringBuilder?
A) StringBuffer is synchronized and StringBuilder is not.
23) How to convert Integer to String?
A) With the help of toString() method of String class.
24) How to convert Integer to String?
A) There are many ways but two ways are shown below:
1) String s = 7 + “ ”;
2) String s = String.valueOf(7);
In the first case, JVM consumes a lot of space for memory. In second case JVM not consume memory.
25) Is a String Thread safe or not?
A) Yes, String is thread safe because string object is immutable that’s way string is thread safe.
26) What is toString() method?
A) If you want to represent an object as a string, toString() method is used.
27) What is the advantage of the toString() method?
A) By overriding the toString() method of the object class, we can returns values of the object.
28) What is difference between compareTo() and equals() method?
|compareTo() method is provided by java.lang.String package that checks the difference string to the character by character if both the strings are same then return 0 otherwise it returns (-value) for each character||Equals() method is java.object package Object class method that checks the two different object and returns true or false.|
29) What is String pool in Java?
A) String pool is a special storage area in java heap, mostly located on Permgen space, to store string literals like “abc”. When Java program creates a new string using a string literal, JVM checks for that string in the pool and, if string literal is already present in the pool then the same object is returned instead of creating a whole new object. String pool check is performed only when you create a string as literal, if you create String using new() operator, a new String object will be created even if the string with the same content is available in the pool.
30) Is String thread-safe in Java?
A) Since the string is immutable, it is a thread safe and it can be shared between multiple threads without external synchronization.
31) Why java uses the concept of the string literal?
A) To make Java more memory efficient, because no new objects are created if it exists already in string constant pool.
32) What is the difference between creating an object of string class using new operator and using doubles quotes?
A) Using double quotes object of the string class representing a specific sequence of characters could be created only once. Whereas using new operator we can create any number of String class objects representing the same sequence of characters.