# Sec 4: String Handling

## 84) What is the meaning of immutable in terms of String?

The simple meaning of immutable is unmodifiable or unchangeable. Once string object has been created, its value can't be changed.

![](https://1241747088-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lpv9LvBSlFaukf_ALqh%2F-Lpv9NPw3Ji1X5Vk8CES%2F-LpvA08MQZ1jv4pkHkB2%2Fsec2_23.png?generation=1569729549772031\&alt=media)

## 85) Why string objects are immutable in java?

Because java uses the concept of **string literal**. Suppose there are 5 reference variables,all referes to one object "sachin".If one reference variable changes the value of the object, it will be affected to all the reference variables. That is why string objects are immutable in java.

## 86) How many ways we can create the string object?

There are two ways to create the string object, by string literal and by new keyword.\
**1) String Literal**

![](https://1241747088-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lpv9LvBSlFaukf_ALqh%2F-Lpv9NPw3Ji1X5Vk8CES%2F-LpvA08O4zvc6BLOSyRp%2Fsec2_24.png?generation=1569729549675771\&alt=media)

**2) By new keyword**

![](https://1241747088-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lpv9LvBSlFaukf_ALqh%2F-Lpv9NPw3Ji1X5Vk8CES%2F-LpvA08QQZHy_V_PE-kf%2Fsec2_25.png?generation=1569729549656591\&alt=media)

## 87) How many objects will be created in the following code?

![](https://1241747088-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lpv9LvBSlFaukf_ALqh%2F-Lpv9NPw3Ji1X5Vk8CES%2F-LpvA08S-GhjzxY0jKFc%2Fsec2_26.png?generation=1569729554860923\&alt=media)

**Only one** object.

## 88) Why java uses the concept of string literal?

To make Java more memory efficient (because no new objects are created if it exists already in string constant pool).

## 89)How many objects will be created in the following code?

![](https://1241747088-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lpv9LvBSlFaukf_ALqh%2F-Lpv9NPw3Ji1X5Vk8CES%2F-LpvA08Um0wiShxvGY3A%2Fsec2_27.png?generation=1569729555875366\&alt=media)

Two objects, one in string constant pool and other in non-pool(heap).

## 90) What is the basic difference between string and stringbuffer object?

String is an **immutable** object. **StringBuffer** is a mutable object.

## 91) What is the difference between StringBuffer and StringBuilder ?

StringBuffer is **synchronized** whereas StringBuilder is not **synchronized**. StringBuffer is **synchronized**( which means it is **thread safe** and hence you can use it when you implement threads for your methods) whereas StringBuilder is not synchronized( which implies it isn’t thread safe).

## 92) How can we create immutable class in java ?

We can also create immutable class by creating final class that have final data members

![](https://1241747088-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lpv9LvBSlFaukf_ALqh%2F-Lpv9NPw3Ji1X5Vk8CES%2F-LpvA08Wt444br8CGDQC%2Fsec2_28.png?generation=1569729553591715\&alt=media)

## 93) What is the purpose of toString() method in java ?

The toString() method returns the string representation of any object. If you print any object, java compiler internally invokes the toString() method on the object. So overriding the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation.
