Previous Next

Interview Questions on Java - Strings

1. What is String?

String is a immutable class which used to represent the multiple Characters.

2. What is the Difference between String and StringBuffer?

String is immutable class.
StringBuffer is not immutable and has extra methods reverse() and delete().

3. What is the output of System.out.println(2+3+"44"+6);

5446

4. What is output of below program String s1= new String("abc");
String s2 = new String("abc");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));

false
true
== checks the memory references
equals method checks the contents

5. What is output of below program
String s3 = "abc";
String s4 = "abc";
System.out.println(s3 == s4);
System.out.println(s3.equals(s4));

true
true
here we did not use the new operator.
So both s3 and s4 points to same object.

6. What is the Difference between StringBuffer and StringBuilder?

StringBuffer is synchronized and String Builder is not Synchronized.


Previous Next