String Flashcards
Is String a keyword in java?
No. String is not a keyword in java. String is a final class in java.lang package which is used to represent the set of characters in java.
Is String a primitive type or derived type?
String is a derived type.
In how many ways you can create string objects in java?
String s1 = new String(“abc”); //Creating string object using new operator
String s2 = “abc”; //Creating string object using string literal
What is string constant pool?
String objects are most used data objects in Java. Hence, java has a special arrangement to store the string objects. String Constant Pool is one such arrangement. String Constant Pool is the memory space in heap memory specially allocated to store the string objects created using string literals. In String Constant Pool, there will be no two string objects having the same content.
What is special about string objects as compared to objects of other derived types?
One special thing about string objects is that you can create string objects without using new operator i.e using string literals. This is not possible with other derived types (except wrapper classes). One more special thing about strings is that you can concatenate two string objects using ‘+’. This is the relaxation java gives to string objects as they will be used most of the time while coding. And also java provides string constant pool to store the string objects.
What do you mean by mutable and immutable objects?
Immutable objects are like constants. You can’t modify them once they are created. They are final in nature. Where as mutable objects are concerned, you can perform modifications to them.
Which is the final class in these three classes – String, StringBuffer and StringBuilder?
All three are final. (Interviewer will ask this type of questions to confuse you)
Why StringBuffer and StringBuilder classes are introduced in java when there already exist String class to represent the set of characters?
The objects of String class are immutable in nature. i.e you can’t modify them once they are created. If you try to modify them, a new object will be created with modified content. This may cause memory and performance issues if you are performing lots of string modifications in your code. To overcome these issues, StingBuffer and StringBuilder classes are introduced in java.
How many objects will be created in the following code and where they will be stored in the memory?
String s1 = “abc”;
String s2 = “abc”;
Only one object will be created and this object will be stored in the string constant pool.
How do you create mutable string objects?
Using StringBuffer and StringBuilder classes. These classes provide mutable string objects.
Which one will you prefer among “==” and equals() method to compare two string objects?
I prefer equals() method because it compares two string objects based on their content. That provides more logical comparison of two string objects. If you use “==” operator, it checks only references of two objects are equal or not. It may not be suitable in all situations. So, rather stick to equals() method to compare two string objects.
Which class will you recommend among String, StringBuffer and StringBuilder classes if I want mutable and thread safe objects?
StringBuffer
How do you convert given string to char array?
Using toCharArray() method.
How many objects will be created in the following code and where they will be stored? String s1 = new String("abc");
String s2 = “abc”;
Here, two string objects will be created. Object created using new operator(s1) will be stored in the heap memory. The object created using string literal(s2) is stored in the string constant pool.
Where exactly string constant pool is located in the memory?
Inside the heap memory. JVM reserves some part of the heap memory to store string objects created using string literals. [more]
I am performing lots of string concatenation and string modification in my code. which class among string, StringBuffer and StringBuilder improves the performance of my code. Remember I also want thread safe code?
StringBuffer class gives better performance in this scenario. As String class is immutable, if you use this class, a new object will be created after every string concatenation or string modification. This will lower the performance of the code. You can use StringBuilder also, but it is not thread safe. So, StringBuffer will be optimal choice here.
What is string intern?
String object in the string constant pool is called as String Intern. You can create an exact copy of heap memory string object in string constant pool. This process of creating an exact copy of heap memory string object in the string constant pool is called interning. intern() method is used for interning.
How many objects will be created in the following code and where they will be stored? String s1 = new String("abc");
String s2 = new String(“abc”);
Two objects will be created and they will be stored in the heap memory.