String Interview Questions Java by Prashant 1669017681
String Interview Questions Java by Prashant 1669017681
com/in/prashant-kumar-76b786168/
https://github.com/prashantt17
@prashantkumar
In how many ways you can create string
objects in java?
String s2 = "hello";
//Creating string object using
string literal
What is string pool?
Whenever you create a string object using string literal, JVM first
checks the content of the object to be created. If there exist an
object in the string pool with the same content, then it returns
the reference of that object. It doesn’t create a new object. If
the content is different from the existing objects then only it
creates new object.
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?
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?
1 String s1 = "sachin";
Only one object will be created and this object will be stored in
the string constant pool.
How do you create mutable string objects?
2 String s2 = "abc";
Inside the heap memory. JVM reserves some part of the heap memory to store string
objects created using string literals.
Whenever you create a string object using string literal, that object is stored in
the string constant pool and whenever you create a string object using new keyword,
such object is stored in the heap memory.
For example, when you create string objects like below, they will be stored in the
String Constant Pool.
1 String s1 = "abc";
2
3 String s2 = "xyz";
4
5 String s3 = "123";
6
7 String s4 = "A";
And when you create string objects using new keyword like below, they will be stored
in the heap memory.
One more interesting thing about String Constant Pool is that, pool space is allocated
to an object depending upon it’s content. There will be no two objects in the pool
having the same content.
This is what happens when you create string objects using string literal,
“When you create a string object using string literal, JVM first checks the content of
to be created object. If there exist an object in the pool with the same content, then
it returns the reference of that object. It doesn’t create new object. If the content is
different from the existing objects then only it creates new object.”
But, when you create string objects using new keyword, a new object is created
whether the content is same or not.
This can be proved by using “==” operator. As “==” operator returns true if two
objects have same physical address in the memory otherwise it will return false. In the
below example, s1 and s2 are created using string literal “abc”. So, s1 == s2 returns
true. Where as s3 and s4 are created using new operator having the same content.
But, s3 == s4 returns false.
?
1 public class StringExamples
2 {
3 public static void main(String[] args)
4 {
5 //Creating string objects using literals
6
7 String s1 = "abc";
8
9 String s2 = "abc";
10
11 System.out.println(s1 == s2); //Output : true
12
13 //Creating string objects using new operator
14
15 String s3 = new String("abc");
16
17 String s4 = new String("abc");
18
19 System.out.println(s3 == s4); //Output : false
20 }
21 }
In simple words, there can not be two string objects with same content in the string
constant pool. But, there can be two string objects with the same content in the
heap memory.
Why You Need String Constant Pool?
intern() Method :
Look at this example. Object s1 will be created in string constant pool as we are using
string literal to create it and object s2 will be created in heap memory as we are using
new operator to create it. When you call intern() method on s2, it returns reference of
object to which s1 is pointing as it’s content is same as s2. It does not create a new
object in the pool. So, S1 == s3 will return true as both are pointing to same object in
the pool.
?
1 public class StringExamples
2 {
3 public static void main(String[] args)
4 {
5 String s1 = "JAVA";
6
7 String s2 = new String("JAVA");
8
9 String s3 = s2.intern(); //Creating String Intern
10
11 System.out.println(s1 == s3); //Output : true
12 }
13 }
What do you mean by ‘String Literals Are
Automatically Interned’ ?
Using interned string, you can save the memory space. If you are
using lots of string objects with same content in your code, than
it is better to create an intern of that string in the pool. Use that
intern string whenever you need it instead of creating a new
object in the heap. It saves the memory space.
Since Strings are very popular as HashMap key, it's important for
them to be immutable so that they can retrieve the value object
which was stored in HashMap. Since HashMap works in the
principle of hashing, which requires same has value to function
properly. Mutable String would produce two different
hashcodes at the time of insertion and retrieval if contents of
String was modified after insertion, potentially losing the value
object in the map.
What is the similarity and difference between
String and StringBuffer class?
https://github.com/prashantt17
@prashantkumar