Java Techies- Solution for All
String Interview Questions
1 - What is String? Is it type of data type or not?
String is a sequence of characters. String is a class in java.String a="Hello";
In above statement "a" is object reference and "Hello" is a string literal. String is used as a data type in java.
2 - Why String is an immutable Object?
String class is immutable, so that once it is created a String object cannot be changed. As string is widely used it create its object in string pool, so that next time any object of string have same value, it reference to same literal in string pool.3 - What is difference between "String" and "new String()" in java?
String s1="Hello";String s2 = new String ("Hello");
First one will create object in string pool and second one creates object in heap memory area.
4 - How to Split String in java?
You can split string by using split() method in String class,Example -
String a = "This is a String Object"; String x[] = a.split(" "); For (i = 0 ; I < x . length ; i++) { System.out.println( x [i] ); }This will split string with space that is when space find string get split and store is an array.
5 - Difference between String, StringBuffer and StringBuilder?
String is a immutable class | StringBuffer is a mutable class | StringBuilder is a mutable class |
New object is created every time when modified | No new object is created, modification is done in existing one | No new object is created, modification is done in existing one |
Syntax - String s1="hello"; | Syntax - StringBuffer sf=new StringBuffer("hello"); | Syntax - StringBuilder sb=new StringBuilder("hello"); |
- | StringBuffer methods are synchronized. | StringBuilder methods are non-synchronized and added in jdk 5. |
Use String if you require immutability | use Stringbuffer in java if you need mutable + thread-safety. | use StringBuilder in Java if you require mutable + without thread-safety.. |
6 - What is String Pool?
String pool is a memory area in which string literals get stores through jvm. A single instance will be used for all double-quoted literals having same literal value. String pool is created in permanent generation memory area.7 - How to concat two different String?
There are to two methods to concat strings.- Use concat() method to concat strings.
Syntax -
String s1="hello";
System.out.println(s1.concat("java")); - Use '+' operator to concat strings
Syntax -
String s1="hello";
System.out.println(s1+" java");
8 - What is StringTokenizer Class?
StringTokenizer class allow to break a string into tokens. This is a lecacy class lies in java.util.StringTokenizer. An instance of StringTokenizer behaves in one of two ways, depending on condition whether it was created with the returnDelims flag having the value true or false.Example
StringTokenizer st = new StringTokenizer("this is java"); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); }
9 - How to work trim() in string?
Trim() method removes whitespace from the front and end of the string.Example -
String a=" hello "; System.out.println(a.trim()+" bye"); Output - hello bye
10 - How will you sort a collection of String in case sensitive order?
Use Collections.sort(list, String.CASE_INSENSITIVE_ORDER);Example -
public class StringSortExample { public static void main(String[] args) { Listlist = new ArrayList (); list.add("Shalabh"); list.add("amit"); list.add("Lalit"); display(list); Collections.sort(list); display(lsit); Collections.sort(list, String.CASE_INSENSITIVE_ORDER); display(list); } Public static void display(List list) { for (String str : list) { System.out.print(str + " "); } } }
11 - How does substring () method works?
substring() method returns a part of string. This method has two variant- substring(int begin index);
- substring(int begin index , int end index)
Example -System.out.println(a.subString(2)); // output - llo System.out.println(a.subString(2,4)); //output - ll Substring divides the string into part.