String class in java questions

Top 20 Java String Interview Questions And Answers

Java String Interview Questions

Java is one of the most popular programming languages. Java is simple, secure, and robust.

When you are ready to face an interview for Java, in most interviews, the starting question will be about Java String.

You should not make any mistakes at the start of your interview.

You only have one chance to make a first impression.
– Stephanie Perkins, Lola, and the Boy Next Door

In this article, I am covering the top 20 Java String Interview Questions. After reading this article, you will be ready to rock the String part of your Java Interview.

1. What is String in Java? Is it a datatype?

The string is a final class in Java defined in java.lang package. You can assign a sequence of characters to a string variable. For example String name = «Gaurav»;

No, String is not a datatype like int , char , or long .

When you assign a sequence of character to String variable, you are creating a string object.

Every String literal is an instance of the String class, and its value can not be changed.

2. What is the difference between String in C language and String in Java?

If your resume contains something related to the ‘C’ language, they can ask you this question.

String in Java and C is completely different. In ‘C’ language String is a null-terminated character array.

In the image given below, I have shown the structure of the string in C and Java.

Showing String in C and Java

Showing String in C and Java

The string is more abstract in Java.

The string class comes with java.lang package and has lots of predefined methods that a programmer can use to operate on a string or get information about a String.

So String is more feature-rich in Java than C.

3. What is the String pool in Java?

The String pool is a special type of memory maintained by the JVM.

String pool is used to store unique string objects.

When you assign the same string literal to different string variables, JVM saves only one copy of the String object in the String pool, and String variables will start referring to that string object.

I have shown the pictorial explanation of the above sentence in the following diagram.

Two string variables pointing to the single string object from string pool

Two string variables pointing to the single string object from the string pool

The purpose of maintaining this special type of memory is memory optimization.

4. Why String is immutable?

In most of the Java Interviews, you will face this question. Why do you think Java language designers kept string immutable?

You can give the following reasons.

Java String pool is possible because the String is immutable.

If you assign the same string literal to many string variables, JVM will save only one copy of the string object in the Java string pool, and these variables will start referring to that string object.

If you were not asked about the String pool before this question, please give a little background about the string pool concept in Java. Please refer to the previous question.

Also, another reason can be Security. We know that almost every Java program contains a string, and it is used to save important data like usernames and passwords. So it should not be changed in-between. Otherwise, there will be a security problem.

5. How many objects will be created from the following code?

By seeing the above code, only two string objects will be created. The first two variables will refer to the same string object with the value «Gaurav» . JVM uses the string pool concept to store only one copy of duplicate string objects to string constant pool.

But when we use a new keyword to create a new string, a new string object will be created and stored in the Java heap memory.

So for the third variable thirdString , a new string object will be created and stored in a Java heap space.

So there will be a total of two objects, one from the Java string pool and one from the Java heap memory.

Below, I have shown these two objects in the following diagram.

Showing the two string objects from the Java string pool and Java heap memory

Showing the two string objects from the Java string pool and Java heap memory

6. What is the intern() method?

intern() method is used to add the unique copy of the string object to the Java string pool manually.

We know, when we create a string using a new keyword, it will be stored in the heap memory.

We can store the unique copy of that string object in the Java string pool using the intern() method.

When you do such a thing, JVM will check if the string object with the same value is present in the string pool or not.

If a string object with the same value is present, JVM will simply provide the reference of that object to the respective string variable.

If a string object with the same value is not present in the string pool, JVM creates a string object with the same value in the String pool and returns its reference to the string variable.

7. What is the difference between the String and StringBuffer ?

The String is a final class in Java. The String is immutable. That means we can not change the value of the String object afterword.

Since the string is widely used in applications, we have to perform several operations on the String object. Which generates a new String object each time, and all previous objects will be garbage object putting the pressure on the Garbage collector.

Hence, the Java team introduced the StringBuffer class. It is a mutable String object, which means you can change its value.

The string is immutable, but the StringBuffer is mutable.

8. What is the difference between the StringBuffer and StringBuilder ?

We know String is immutable in Java. But using StringBuffer and StringBuilder , you can create editable string objects.

When Java Team realizes the need for the editable string object, they have introduced the StringBuffer class. But all the methods of the StringBuffer class are synchronized. That means at a time, only one thread can access a method of the StringBuffer .

As a result, it was taking more time.

Latter, Java Team realizes that making all methods of the StringBuffer class synchronized was not a good idea, and they introduced a StringBuilder class. None of the methods of the StringBuilder class are synchronized.

Since all the methods of the StringBuffer class are synchronized, StringBuffer is thread-safe, slower, and less efficient as compared to StringBuilder .

Since none of the methods of the StringBuilder class is synchronized, StringBuilder is not thread-safe, faster, and efficient as compared to StringBuffer .

9. Can we compare String using the == operator? What is the risk?

Yes, of course, we can compare String using the == operator. But when we are comparing string using the == operator, we are comparing their object reference, whether these string variables are pointing towards the same string object or not.

Most of the time, developers want to compare the content of the strings, but mistakenly they compare strings with == operator, instead of equals() method, which leads to an error.

Below, I have given a program, which shows the string comparison using the == operator and equals() method.

Источник

Читайте также:  font-family
Оцените статью