Skip to main content

Command Palette

Search for a command to run...

Chapter7: Strings and StringBuilder

Published
3 min readView as Markdown
Chapter7: Strings and StringBuilder
S

Passionate about Full-Stack Development | DSA Learner | Enthusiastic about Technology | 👩‍🎓St. Joseph's Institute of Technology, 3rd Year

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.

//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.

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.

use case: All the similar values of strings are not recreated in the pool.

Ans:

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.

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.

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.

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.

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.

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));

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