Java String Interview Questions

What is String pool?

String pool is an area in java heap memory and is a pool of strings. 

How we can create String?

Answer-  String can be created using new keyword similar to other objects and also by assigning content in a double quotes.

  1. String str1= new String("java");
  2. String str2="java";

What is the difference between String created using new keyword and by assigning content in a double quotes?

Any String created using double quotes are created in String pool and if string with same content is available in pool, it will use that. In case of String created with new keyword, String is not stored in a String pool.    

Is String class thread safe?

Yes String is thread safe

What is intern() method of String class?

intern() method of String always returns a String from a String pool if it is available and will create a new one if not available. 

Is String a final class?

Yes, String is a final class.

What is difference between String and StringBuffer?

String is an immutable class where as StringBuffer is not.

What is difference between StringBuffer and StringBuilder?

StringBuffer is thread safe because its methods are synchronized whereas StringBuilder is not thread safe.

Does String provide any method to get a bytes?

Yes, we can call getBytes() method. 

 

 

 

 

 

 

 

 

          

Like us on Facebook