Are you prepared in attending an interview? Then do not worry, we’ve a right answer for your job interview preparation. If you are preparing for Java String job interview and don’t know how to crack interview and what level or difficulty of questions to be asked in job interviews then go through Wisdomjobs Java String interview questions and answers page to crack your job interview. In java, string is basically an object that represents sequence of char values. Java String class provides a lot of methods to perform operations on string such as compare(), concat(), equals(), split(), length(), replace(), substring() etc. Below is the list of frequently asked Java String interview questions and answers which gets you ready to face the interviews:
In Java String class represents character strings which means; Strings in Java are objects and all strings are instances of the String class. Internally in String class Strings are stored as character array.
Since strings are objects so strings can of course be created using new operator. String class has more than 10 constructors to create Strings which ranges from taking nothing as parameter to taking char array, StringBuffer, StringBuilder, another String as argument.
Another and more preferred way to create Strings is to assign String literal directly to a String reference as you will do for any primitive type. For every string literal Java will automatically constructs a String object.
As example - String str = “abc”;
No. For every string literal Java automatically constructs a String object.
When String literals are created they are stored in a String pool and that is a common pool; which means if there are two strings literals having the same content then those string will share the space in the pool.
When String object is created by assigning a string literal, pool will be checked to verify if there is any existing object with the same content if there is then that existing reference is used, no new object is created in that case. If no object is found with the same content then this new literal will be added in the pool.
String pool is stored in the heap.
An immutable object is an object that would not be able to change its state after creation. Thus immutable object can only be in one state and that state can not be changed after creation of the object.
Yes String object is immutable. Once you create a String object the content of that string cannot be modified.
Since Java maintains a string pool where String references are shared thus changing content of any of the String will also affect the other strings sharing the same references that’s one reason why string is immutable.
Since String is immutable, whenever you perform any operation on string which alters its content a new string object is created containing the modified string. Which means all the methods of the String class that modify the content in any way return a new String object with the modified content.
Now, What if you can override the method of the String class so that it modifies and return the original string reference itself? In that case all the other strings having the same data in the string pool will also get affected as the reference is shared for the String literals having the same content.
To avoid these kind of scenarios String class is declared as final and it can’t be overridden.
‘+’ operator is overloaded in Java for String. It is used for concatenating two strings.
Since s1 and s2 are string literals and having the same content object reference will be shared by them in the string pool. Therefore only one object is created.
s1 will go to string pool, for s2 new object is created. S3, though created using new will still search in the string pool for any reference having the same content as intern() method is used. So two objects will be created.
Using intern() method you can still get string object from the pool (if it exists) even if new operator is used to create a 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.
Yes string is thread safe in Java as String is immutable.
StringBuffer class is the companion class of String. StringBuffer is a mutable(modifiable) sequence of characters which is in contrast to String class which is an immutable sequence of characters. Thus in case of StringBuffer length and content of the sequence can be changed through certain method calls.
Since StringBuffer is mutable a new String object is not created every time string is modified, which in turn results in less memory consumptions and not having lots of intermediate String object for garbage collection.
StringBuilder class (Added in Java 5),just like StringBuffer, is a mutable(modifiable) sequence of characters which is in contrast to String class which is an immutable sequence of characters. Thus in case of StringBuilder length and content of the sequence can be changed through certain method calls.
String is immutable where as both StringBuffer and StringBuilder are mutable.
String and StringBuffer are thread safe where as StringBuilder is not thread safe.
No, StringBuffer is not immutable.
Yes, StringBuffer class is final in Java.
Yes StringBuffer class is thread safe. Methods in StringBuffer class are synchronized.
No StringBuilder class is not thread safe. That makes it faster than StringBuffer.
Yes StringBuilder class is final in Java.
equals() method can be used for comparing two strings in Java. If you want to ignore case then you can use equalsIgnoreCase(String anotherString) method.
There are also compareTo() and compareToIgnoreCase() methods for comparing two strings lexicographically. Returns an integer indicating whether this string is greater than (result is > 0), equal to (result is = 0), or less than (result is < 0) the argument.
You can also use matches() method where you can pass a regular expression for matching strings.
“==” operator will compare the references of the strings not the content.
String str1 = "abc";
String str4 = new String("abc");
Comparing these two strings using “==” operator
if(str1 == str4)
will return false as the references are different.
You can get the character at a particular index within a string by invoking the charAt() accessor method.
String str = "Example String";
char resChar = str.charAt(3);
Will give char ‘m’. If you want to get more than one consecutive character from a string, you can use the substring method. The substring method has two versions -
To find characters or substrings with in a string indexOf() and lastIndexOf() methods can be used.
You can also use contains() method
public boolean contains(CharSequence s) - Returns true if and only if this string contains the specified sequence of char values. Otherwise it returns false.
String provides a split method in order to split the string into one or more substring based on the given regular expression.
As example If you have a string where one (or more) spaces are used and you want to split it around those spaces.
String str1 = "split example program";
String strArray = str1.split("\s+");
With Java 8 join() method has been added in the String class which makes it very easy to join the multiple strings.
join method has two overloaded versions -
Yes from Java 7 string can be used in switch case statement.
Java String Related Tutorials
|Adv Java Tutorial||J2EE Tutorial|
|Core Java Tutorial||JSP Tutorial|
|Java-Springs Tutorial||Java Tutorial|
|Java 8 Tutorial|
Java String Related Interview Questions
|Adv Java Interview Questions||J2EE Interview Questions|
|Core Java Interview Questions||JSP Interview Questions|
|Java-Springs Interview Questions||JMS(Java Message Service) Interview Questions|
|Java applet Interview Questions||Java Interview Questions|
|Java 8 Interview Questions||Java Programmer Interview Questions|
Presentation Tier Design Considerations And Bad Practices
Business Tier Design Considerations And Bad Practices
J2ee Patterns Overview
Presentation Tier Patterns
Business Tier Patterns
Epilogue J2ee Patterns Applied
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.