# Chapter7: Strings and StringBuilder

### **Overview on String**

Every string that you create is actually a object of type string. Strings in Java are immutable, meaning their values cannot be changed once they are created. Operations such as concatenation or substring extraction result in the creation of new string objects rather than modifying existing ones. To create a string in Java, one can use the String literal or the `new` keyword to instantiate a String object. Java also provides various methods for comparing strings, searching for substrings, and converting between different data types. The immutability of strings ensures their stability, making them a fundamental and widely used data type in Java programming.

```java
//for eg:
          String  name = "kavya";
//        (type)  (ref    (object)
//              variable)
```

### What if two different ref variables have similar object?

When two different ref variables have similar object, how does the object get stored in the heap memory?. The below picture will give a clear scenario of the question mentioned above.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705590088380/86232e5b-6b4a-4936-abeb-723384bc5836.jpeg align="center")

Before finding solution for the above raised question, lets get to know 2concepts that are inter related.

Concepts:

1. String pool
    
2. Immutability
    

***String pool:*** Separate memory structure inside the heap.

<mark>use case: All the similar values of strings are not recreated in the pool.</mark>

Ans:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705590675494/ca23a019-8d58-4358-8818-90e0f56c8ac1.jpeg align="center")

What if you try to change value of a object by another name(like a = kunal to kavya), would it be changed for b also or not? (like for arrays).

Over here i do not change the object, i create new object.

```java
String a="kavya";
String b="kavya";
System.out.print(a); //prints kavya
a="kavya s";
System.out.print(a); //prints kavya s
```

Ans: ***No, because of Immutability.***

\* STRINGS ARE **IMMUTABLE** (due to some security reasons).

I hope now you must have got an idea about, how two ref variables having similar objects will be stored in the heap.

### **Comparison of Strings**

1. `==` method
    

`==` checks if reference variables are pointing to same object.

```java
String a = "kavya";
String b = "kavya";
System.out.println(a == b); //prints true
```

### **How to create different objects of same values?**

initializes a newly created String object.

```java
String name1 = new String("kavya");
String name2 = new String("kavya");
```

Over here it is actually creating these values outside the pool but in the heap.

So, here when you use `==` method to compare(name1==name2) will give *False.*

```java
String name1 = new String("kavya");
String name2 = new String("kavya");
System.out.println(name1 == name2); //prints False
```

So when you only need to check the value, use `.equals()` method to compare.

```java
String name1 = new String("kavya");
String name2 = new String("kavya");
System.out.println(name1.equals(name2); //prints True.
```

### **Accessing characters in string** \*`variable_name(charAt(index));`

```java
String a = "kavya";
String b = "kavya";
System.out.println(a.charAt(2));  //prints v
```
