Tutorials

Java String Interview Questions


Java String Interview Question 2


What is difference between String and StringBuffer in java?

The major difference between String and StringBuffer is:

String StringBuffer
String is immutable in nature. StringBuffer is mutable in nature
String class overrides equals() method of object class StringBuffer class doesn't overrides equals() method of object class

Note: same answer applies for String vs StringBuilder 


What is difference between StringBuilder and StringBuffer in java?

The major difference between StringBuilder and StringBuffer is:

StringBuilder StringBuffer
StringBuilder is not thread safe or synchronized in nature StringBuffer is thread safe or synchronized in nature
As StringBuilder isn't thread safe thus multiple threads can call the same method of this class simultaneously.  As StringBuffer is thread safe so multiple threads cannot call same method of this class simultaneously
StringBuilder being non-synchronized in nature is faster and more efficeint than StringBuffer  StringBuilder being synchronized in nature is slow and less efficient than StringBuilder

 


Why do we override toString() in java?

In java, overriding toString() becomes necessary if we want to return the String representation of the object.

 

 


How do we compare two Strings in java?

In java, we can can compare String in following ways:

  • using equals() method
  • using == operator
  • using compareTo() method

 


How would you split a String at given delimiter?

To split a String at a delimiter, you can use any one of the following:



Please Share this page
Views : 35
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!