0% found this document useful (0 votes)
5 views5 pages

String Handling

The document discusses various aspects of strings in Java, including the concept of the string pool, immutability, and the differences between String, StringBuffer, and StringBuilder. It explains the reasons for string immutability, the significance of the string constant pool, and the performance implications of using different string classes. Additionally, it covers how to create immutable classes and the differences between string comparison methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

String Handling

The document discusses various aspects of strings in Java, including the concept of the string pool, immutability, and the differences between String, StringBuffer, and StringBuilder. It explains the reasons for string immutability, the significance of the string constant pool, and the performance implications of using different string classes. Additionally, it covers how to create immutable classes and the differences between string comparison methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1-What is string pool?

 String pool is nothing but a storage area in Java heap where string literals stores.
 It is also known as String Intern Pool or String Constant Pool.
 It is just like object allocation. By default, it is empty and privately maintained by the Java
String class.
 Whenever we create a string the string object occupies some space in the heap memory.
 Creating a number of strings may increase the cost and memory too which may reduce the
performance also.
2-How to create Immutable class?

 The class must be declared as final so that child classes can’t be created.
 Data members in the class must be declared private so that direct access is not allowed.
 Data members in the class must be declared as final so that we can’t change the value of it
after object creation.
 A parameterized constructor should initialize all the fields performing a deep copy so that
data members can’t be modified with an object reference.
 Deep Copy of objects should be performed in the getter methods to return a copy rather
than returning the actual object reference)

3-Difference B/w StringBuilder and String Buffer?

Immutability:

This is main reason why StringBuffer and StringBuilder are introduced. As objects of String class are
immutable, objects of StringBuffer and StringBuilder class are mutable. You can change the contents
of StringBuffer and StringBuider objects at any time of execution. When you change the content, new
objects are not created. Instead of that the changes are applied to existing object. Thus, solving
memory issues may cause by String class.

Object Creation:

You have to use ‘new ‘operator to create objects to StringBuffer and StringBuilder classes. You can’t
use string literals to create objects to these classes. For example, you can’t write StringBuffer sb =
“JAVA” or StringBuilder sb = “JAVA”. It gives compile time error. But you can use both string literals
and new operator to create objects to String class.

Storage Area:

As objects of StringBuffer and StringBuilder are created using only new operator, they are stored
in heap memory. Whereas objects of String class are created using both string literals and new
operator, they are stored in string constant pool as well as heap memory.

Thread Safety:

Any immutable object in java is thread safety. Because they are unchangeable once they are created.
Any type of thread can’t change the content of immutable object. This applies to objects of String
class also. Of the StringBuffer and StringBuilder objects, only StringBuffer objects are thread safety.
All necessary methods in StringBuffer class are synchronized so that only one thread can enter into
its object at any point of time. Whereas StringBuilder objects are not thread safety.
Performance:

Because of thread safety property of String and StringBuffer classes, they reduce the performance of
multithreaded applications. Because, multiple threads can’t enter into objects of these
classes simultaneously. One thread has to wait until another thread is finished with them. But you
will not find performance problems if you use StringBuilder class. Because, multiple threads can
enter into objects of this class. But, be aware that StringBuilder is not thread safety.

String Concatenation:

There will be serious performance issues when you are performing lots of string concatenation using
String class. This is because, each time you perform string concatenation using string class, a new
object will be created with the concatenated string. This slows down an application. But, if you use
either StringBuffer or StringBuilder instead of String class, your application will perform better. Below
program shows time taken by all three classes to perform string concatenation 10000 times.

4-Why String is Immutable?

The String is immutable in Java because of the security, synchronization and concurrency, caching,
and class loading. The reason of making string final is to destroy the immutability and to not allow
others to extend it.

The String objects are cached in the String pool, and it makes the String immutable. The cached
String literals are accessed by multiple clients. So, there is always a risk, where action performs by
one client affects all other clients. For example, if one client performs an action and changes the
string value from Pressure to PRESSURE, all remaining clients will also read that value. For the
performance reason, caching of String objects was important, so to remove that risk, we have to
make the String Immutable.

5- Do you have any idea why strings have been made immutable in Java?

 Immutable strings increase security. As they can’t be modified once they are created, so we
can use them to store sensitive data like username, password etc.
 Immutable strings are thread safe. So, we can use them in a multi-threaded code without
synchronization.
 String objects are used in class loading. If strings are mutable, it is possible that wrong class is
being loaded as mutable objects are modifiable.

6-What is string intern?

String object in the string constant pool is called as String Intern. You can create an exact copy of
heap memory string object in string constant pool. This process of creating an exact copy of heap
memory string object in the string constant pool is called interning. intern () method is used for
interning.

7-Which one will you prefer among “==” and equals () method to compare two string objects?

I prefer equals () method because it compares two string objects based on their content. That
provides more logical comparison of two string objects. If you use “==” operator, it checks only the
references of two objects not their content. It may not be suitable in all situations. So, rather stick to
equals () method to compare two string objects.
8-What is string constant pool?

 String objects are most used data objects in Java. Hence, Java has a special arrangement to
store the string objects. String Constant Pool is one such arrangement. String Constant Pool
is the memory space in the heap memory specially allocated to store the string objects
created using string literals. In String Constant Pool, there will be no two string objects
having the same content.
 Whenever you create a string object using string literal, JVM first checks the content of the
object to be created. If there exist an object in the string constant pool with the same
content, then it returns the reference of that object. It doesn’t create a new object. If the
content is different from the existing objects, then only it creates new object.

9-How many ways you can create string objects in Java?

There are two ways to create string objects in Java. One is using new operator and another one is
using string literals. The objects created using new operator are stored in the heap memory and
objects created using string literals are stored in string constant pool.

String s1 = new String("ABC");

String s2 = "ABC";

10-What do you think about string constant pool? Why they have provided this pool as we can
store string objects in the heap memory itself?

Ans-String constant pool increases the reusability of existing string objects. When you are creating a
string object using string literal, JVM first checks string constant pool. If that object is available in
string constant pool, it returns reference of that object rather than creating a new object. This will
speed up your application as only reference is returned. And it also saves the memory as no two
objects with same content are created.

11-What is the similarity and difference between String and String Buffer class?

The main similarity between String and String Buffer class is that both are thread safe. The main
difference between them is that String objects are immutable whereas String Buffer objects are
mutable.

12-What is the similarity and difference between String Buffer and StringBuilder class?

The main similarity between String Buffer and StringBuilder class is that both produces mutable
string objects. The main difference between them is that String Buffer class is thread safe whereas
StringBuilder class is not thread safe.

13. What do you mean by mutable and immutable objects?

Immutable objects are like constants. You can’t modify them once they are created. They are final in
nature. Whereas mutable objects are concerned, you can perform modifications on them.
14. Why String Buffer and StringBuilder classes are introduced in Java when there already exist
String class to represent the set of characters?

The objects of String class are immutable in nature. i.e. you can’t modify them once they are created.
If you try to modify them, a new object will be created with modified content. This may cause
memory and performance issues if you are performing lots of string modifications in your code. To
overcome these issues, Sting Buffer and StringBuilder classes are introduced in Java.

15. I am performing lots of string concatenation and string modification in my code. which class
among string, String Buffer and StringBuilder improves the performance of my code. Remember I
also want thread safe code?

String Buffer class gives better performance in this scenario. As String class is immutable, if you use
this class, a new object will be created after every string concatenation or string modification. This
will slow down the code. You can use StringBuilder also, but it is not thread safe. So, String Buffer will
be optimal choice here.

16. Do you have any idea why strings have been made immutable in Java?

a) Immutable strings increase security. As they can’t be modified once they are created, so we can
use them to store sensitive data like username, password etc.

b) Immutable strings are thread safe. So, we can use them in a multi-threaded code without
synchronization.

c) String objects are used in class loading. If strings are mutable, it is possible that wrong class is
being loaded as mutable objects are modifiable.

17. What do you think about string constant pool? Why they have provided this pool as we can
store string objects in the heap memory itself?

String constant pool increases the reusability of existing string objects. When you are creating a
string object using string literal, JVM first checks string constant pool. If that object is available in
string constant pool, it returns reference of that object rather than creating a new object. This will
speed up your application as only reference is returned. And it also saves the memory as no two
objects with same content are created.

18) How Java 8 StringJoiner and String.join() method differ from each other?

StringJoiner class internally uses StringBuilder class to join the strings. It is placed in java.util package.
Using Java 8 StringJoiner, you can join only the strings, but not the array of strings or list of strings.

String.join() method internally uses StringJoiner class. This method can be used to join strings or
array of strings or list of strings, but only with delimiter not with prefix and suffix.

You might also like