String Handling Interview Questions in Java
Interview Questions on String Handling in Java
Why use string handling in Java
The basic aim of String Handling concept is storing the string data in the main memory (RAM), manipulating the data of the String, retrieving the part of the String etc. String Handling provides a lot of concepts that can be performed on a string such as concatenation of string, comparison of string, find sub string etc.
Difference between String and StringBuffer
|1||The data which enclosed within double quote (" ") is by default treated as String class.||The data which enclosed within double quote (" ") is not by default treated as StringBuffer class|
|2||String class object is immutable||StringBuffer class object is mutable|
|3||When we create an object of String class by default no additional character memory space is created.||When we create an object of StringBuffer class by default we get 16 additional character memory space.|
When we use String, StringBuffer and StringBuilder
- If the content is fixed and would not change frequently then we use String.
- If content is not fixed and keep on changing but thread safety is required then we use StringBuffer
- If content is not fixed and keep on changing and thread safety is not required then we use StringBuilder
What is Similarities between String and StringBuffer
- Both of them are belongs to public final. so that they never participates in inheritance that is is-A relationship is not possible but they can always participates in As-A and Uses-A relationship.
- We can not override the method of String and StringBuffer.
Difference between StringBuffer and StringBuilder
All the things between StringBuffer and StringBuilder are same only difference is StringBuffer is synchronized and StringBuilder is not synchronized. synchronized means one thread is allow at a time so it thread safe. Not synchronized means multiple threads are allow at a time so it not thread safe.
|1||It is thread safe.||It is not thread safe.|
|2||Its methods are synchronized and provide thread safety.||Its methods are not synchronized and unable to provide thread safety.|
|3||Relatively performance is low because thread need to wait until previous process is complete.||Relatively performance is high because no need to wait any thread it allows multiple thread at a time.|
|1||Introduced in 1.0 version.||Introduced in 1.5 version.|
What is StringTokenizer ?
It is a pre defined class in java.util package can be used to split the given string into tokens (parts) based on delimiters (any special symbols or spaces).