All About Strings in 2023 | Interview Questions

All About Strings in 2023 | Interview Questions

ยท

5 min read

Hey Guys! Welcome back to my blog. Before discussing today's topic I request you to follow my newsletter so that you can get the informative technical blog right in your main feed.

Now, let's start we the topic-Strings. It is one of the essential topics and if you are a programmer you cannot skip it. In this article, we will be going in-depth into strings and learning how it is working internally.

What is a String?

The Office gif. Steve Carell as Michael Scott purses his lips and raises his eyebrows in annoyance as he says, "What?"

The string is nothing but a data representation in text form. If we talk about the java language, String is an object or we can say a class. But now a lot of you will say the string is a data type. The answer would be Yes! The string is a non-primitive data type.

Resolving the confusion between Primitive and Non Primitive data types

If you find it confusing what is primitive and what is non-primitive then this shortcut is for you. If the datatype starts with a capital letter then it's a non-primitive data type. For example Array, Class, String, and Interface. Primitive data types are int, boolean, float, etc.

Video gif. A beaming young boy sits at a table with his hands clenched. His smile grows into an excited face as he feverishly interlocks his fingers.

How do Strings work internally?

Have you ever wondered what happens to the memory when you initialize a string? It's okay, I will be explaining every single detail of it.

You must know that every string you create is an object of type string when you write String name = "Ishan Gaur"; Here name - the reference variable, Obect - "Ishan Gaur" and Datatype - String.

Here two terms or concepts come into the picture -

  1. String Pool

  2. Immutability

How does String gets stored internally?

When you initialize a string, the reference variable gets stored in stack memory, and the object will be stored inside the string pool in Heap memory.

What is String Pool?

The string pool is a separate memory structure inside the heap. now the question is why do we need it? So the answer will be, all the similar values of the string will not be recreated inside the heap.

So if you create another object like String nameTwo = "Ishan Gaur"; This will be pointed towards the string pool object "Ishan Gaur".

This will help to make our program more optimized. How? If we want to create 10 reference variables of "Ishan", it will not make 10 objects of "Ishan" in the heap. It will make one object in the pool and point every similar one to the same pool.

Working In The Zone GIF by 60 Second Docs

Question - If I change one object of a reference variable it will get changed for all 9 reference variables

Answer - The answer to your doubt will be, You cannot change. and here immutability comes into the picture.

What is Immutability & Why Strings are immutable?

Immutability says if you made two reference variables of the same object, you cannot change the object of a particular reference variable.

If you are changing it, it will make a new object in the heap and the reference variable will be pointing to that object.

Now why strings are immutable? The answer is because of security reasons. If you have stored passwords of people and there are 4 people named Ishan. Now if one of the people changes his name to Alexa, because of lack of immutability all three people's names will be changed to Alexa.

That is why immutability is needed.

TV gif. Close up on Jane Levy as Zoey Clarke on Zoeyโ€™s Extraordinary Playlist. Her mouth is wide open in shock and she blinks her eyes, unable to believe what she just saw. She turns and leaves, still completely stunned.

How to compare the Strings?

Now let's discuss a very important topic which is a comparison in strings. Now let's see a scenario.

There are two ways to compare objects in strings

// Suppose you write 
String name = "Ishan gaur"
String name_2 = "Ishan gaur"
System.out.println(name==name_2);
// Tell me what will be the output
Output:
True

As you know that because values are the same both reference variables will be pointing toward the string pool. That is why if you use the "==" operator, the output will be "true".

Note: == This operator checks if the reference variable is pointing to the same object.

Now the question will be how to create different objects of the same value. The answer will be by New keyword.

You can create different objects of the same value like this -

Creating different object of same value
String a = new String("Ishan")
String b = new String("Ishan")
System.out.println(a==b);
// Tell me what will be the output
Output:
False

This is the way and now if you use the == operator to compare it will give false as an output, why? Because now both the object were made out of the string pool.

Now the question will be what do we do to check the values of the object because the == operator is showing false. The answer will be the (.equals) method.

Creating different object of same value
String a = new String("Ishan")
String b = new String("Ishan")
System.out.println(a.equals(b));
// Tell me what will be the output
Output:
True

What is the significance of the toString Method?

The toString() method is majorly used to get the string representation of the object. Whenever you want to print an object the toString() method is get called by the java compiler.

This method can be overridden means you can make your own toString method when there is a need. For example

When we want to return an array we use the toString method of the Array. So to get an array returned we use Arrays.toString() method.

Thank You ๐Ÿ™

I hope you now understood the fundamentals of the strings. If you like this blog please do comment. Also, follow the newsletter below.

Celebrity gif. Musician Kevin Abstract of Brockhampton pats his heart and points to the viewer in a gesture of love and respect. Magenta and yellow squiggly lines explode from his hand as he points.

Did you find this article valuable?

Support Ishan Gaur by becoming a sponsor. Any amount is appreciated!

ย