Core Java Interview - Part 2

1.) What is String in Java?

String is a class in Java.

2.) Explain the ways of String creation in Java?

There are two ways of String creation in Java.

First and recommend way of creating String using String literal, because string objects created with this method are stored in String pool. If any string object with 

same value is already present in pool it return the reference only, otherwise create a new object in string pool and returns the reference. 

String stringLiteral="String Literal";

Second we can create string object in the same way other class objects are created with 'new' operator. Objects created with new operator are not stored in string 

pool. With this method it will always create a new string object.

String stringNew = new String("String with new operator");


3.) What is String pool? What Strng's intern() method do?

String pool is specific memory area to store string objects created by string literal method. 

String is the most used class in Java. String is used in every Java program. So everytime creating a new string will use very much memory and makes the system slow in 

long run. String pool is created to solve this issue.

intern() method - String's intern() method is used to add the string objects in string pool, which are not created with string literal method.   


4.) How to compare two String objects in Java?

String class objects can be compared with two ways, first with "==" operator like primitives and second with equals() method.

Comparison with "==" operator is also called shallow comparison and it only compares if two string objects are pointing the same objects or not.

equals() method compare the actual values of both string objects and return true if both are equal.


5.) What is an immutable class? Is String in Java is immutable?

Immutable class objects can not modified after creation. 
String is an immutable class. So string objects can not modified after creation. If we try to do any modification it will always creates a new string object.

To understand immutability in details check Immutable Class.

6.) What is the difference between String, StringBuffer and StringBuilder?

String - String is an immutable and final class of Java. String objects can not modified after creation. If we try to do some operations on string object it will always 

create a new object. That will increase the memory uses and makes the system slow.

StringBuffer - StringBuffer is a mutable class, So objects of StringBuffer class can modified. All methods in StringBuffer class are synchronized that's why StringBuffer is thread safe and can be used in multi-threaded applications. StringBuffer is slower because of synchronization.

StringBuilder - StringBuilder class was added in Java5, because StringBuffer was slow in performance. StringBuilder is mutable but its methods are not synchronized. StringBuilder is best to use in single threaded application. StringBuilder is faster than StringBuffer. 

No comments:

Post a Comment