Java Interview Questions on String
What is the meaning of Immutable in the context of String class in Java?
Why a String object is considered immutable in java?
How many ways are there in Java to create a String object?
What is String interning?
Why Java uses String literal concept?
What is the basic difference between a String and StringBuffer object?
How will you create an immutable class in Java?
What is the use of toString() method in java?
Arrange the three classes String, StringBuffer and StringBuilder in the order of efficiency for String processing operations?
ANSWERS / HINTS
1- In java, an Immutable object cannot be modified or changed. String is an Immutable class in Java.
Once a String object is created, it cannot be changed and when we assign the String to a new value, a new object is created.
2- In java language String is used for a variety of purposes. For this, it has marked String Immutable.Concept of String literal in Java. Suppose there are two String variables A and B that reference to a String object “TestData”. All these variables refer to the same String literal. If one reference variable A changes the value of the String literal from “TestData” to “RealData”, then it will affect the other variable as well. Due to which String is considered Immutable. In this case, if one variable A changes the value to “RealData”, then a new String literal with “RealData” is created and A will point to new String literal. While B will keep pointing to “TestData”.
3- Java provides 2 ways to create a String object. One is by using String Literal and the other is by using the new operator.
4- String interning is a concept of using only one copy of a distinct String value that is Immutable. It provides the advantage of making String processing efficient in Time as well as Space complexity. But it introduces extra time in the creation of String.
5- Java uses String literal concept to make Java more efficient in memory. If the same String already exists in String constant pool, it can be reused. This saves memory usage.
6- String is an immutable object and Its value cannot change after creation. StringBuffer is a mutable object. In java, we can keep appending or modifying the contents of a StringBuffer.
7- In Java, we can declare a class final to make it immutable. There are following detailed steps to make it Immutable:
Add final modifier to class to prevent it from getting extended
Do not provide any setter methods for member variables
In clone method, return a copy of object instead of the actual object reference
Add private modifier to all the fields to prevent direct access
Use Deep Copy to initialize all the fields by a constructor
Add final modifier to all the mutable fields to assign value only once
8- This method can be used to return the String representation of an Object. When we print an object, Java implicitly calls toString() method. Java provides a default implementation for toString() method. But we can override this method to return the format that we want to print.
9- Decreasing order of efficiency is: StringBuilder, StringBuffer, String
Check Interview Questions on Java
Wikipedia Java
Helpful Thanks!!
Pingback: API TESTING INTERVIEW PART- 1 - Software Testing Questions