Java面试题每日10问(12)
1. What is String Pool?
- String pool is the space reserved in the heap memory that can be used to store the strings.
- The main advantage of using the String pool is whenever we create a string literal; the JVM checks the “string constant pool” first.
- If the string already exists in the pool, a reference to the pooled instance is returned.
- If the string doesn’t exist in the pool, a new string instance is created and placed in the pool. Therefore, it saves the memory by avoiding the duplicacy.
2. What is the meaning of immutable regarding String?
The simple meaning of immutable is unmodifiable or unchangeable.
In Java, String is immutable, i.e., once string object has been created, its value can’t be changed.
class Testimmutablestring{
public static void main(String args[]){
String s="Sachin";
s.concat(" Tendulkar");//concat() method appends the string at the end
System.out.println(s);//will print Sachin because strings are immutable objects
}
}
Sachin
two objects are created but s reference variable still refers to “Sachin” not to “Sachin Tendulkar”.
But if we explicitly assign it to the reference variable, it will refer to “Sachin Tendulkar” object.
3. Why String objects are immutable in Java?
- Because Java uses the concept of the string literal.
- Suppose there are five reference variables, all refer to one object “sachin”.
- If one reference variable changes the value of the object, it will be affected by all the reference variables.
- That is why string objects are immutable in java
Following are some features of String which makes String objects immutable.
- ClassLoader:
-
A ClassLoader in Java uses a String object as an argument. Consider, if the String object is modifiable, the value might be changed and the class that is supposed to be loaded might be different.
-
To avoid this kind of misinterpretation, String is immutable.
-
Thread Safe:
As the String object is immutable we don’t have to take care of the synchronization that is required while sharing an object across multiple threads.
-
Security:
- As we have seen in class loading, immutable String objects avoid further errors by loading the correct class.
- This leads to making the application program more secure. Consider an example of banking software.
- The username and password cannot be modified by any intruder because String objects are immutable.
- This can make the application program more secure.
- Heap Space:
- The immutability of String helps to minimize the usage in the heap memory.
- When we try to declare a new String object, the JVM checks whether the value already exists in the String pool or not.
- If it exists, the same value is assigned to the new object.
- This feature allows Java to use the heap space efficiently.
4.How many ways can we create the string object?
1)String Literal
Java String literal is created by using double quotes.
String s="welcome";
- Each time you create a string literal, the JVM checks the “string constant pool” first.
- If the string already exists in the pool, a reference to the pooled instance is returned.
- If the string doesn’t exist in the pool, a new string instance is created and placed in the pool.
- String objects are stored in a special memory area known as the string constant pool
2) By new keyword
String s=new String("Welcome");//creates two objects and one reference variable
- JVM will create a new string object in normal (non-pool) heap memory, and the literal “Welcome” will be placed in the constant string pool.
- The variable s will refer to the object in a heap (non-pool).
5. How many objects will be created in the following code?
String s1="Welcome";
String s2="Welcome";
String s3="Welcome";
Only one object will be created using the above code because strings in Java are immutable.
🔔Note:
The java.lang.String class implements Serializable, Comparable and CharSequence interfaces.
CharSequence Interface
The CharSequence interface is used to represent the sequence of characters. String, StringBuffer and StringBuilder classes implement it. It means, we can create strings in Java by using these three classes.
6. What is String in Java?
- Generally, String is a sequence of characters. But in Java, string is an object that represents a sequence of characters.
- The java.lang.String class is used to create a string object.
7. Why java uses the concept of the string literal?
To make Java more memory efficient (because no new objects are created if it exists already in the string constant pool).
8. How many objects will be created in the following code?
String s = new String("Welcome");
Two objects, one in string constant pool and other in non-pool(heap).
9.What are the differences between String and StringBuffer?
String | StringBuffer |
---|---|
The String class is immutable. | The StringBuffer class is mutable. |
The String is slow and consumes more memory when you concat too many strings because every time it creates a new instance. | The StringBuffer is fast and consumes less memory when you cancat strings. |
The String class overrides the equals() method of Object class. So you can compare the contents of two strings by equals() method. | The StringBuffer class doesn’t override the equals() method of Object class. |
10.What are the differences between StringBuffer and StringBuilder?
StringBuffer | StringBuilder |
---|---|
StringBuffer is synchronized, i.e., thread safe. It means two threads can’t call the methods of StringBuffer simultaneously. | StringBuilder is non-synchronized,i.e., not thread safe. It means two threads can call the methods of StringBuilder simultaneously. |
StringBuffer is less efficient than StringBuilder. | StringBuilder is more efficient than StringBuffer. |