Wednesday, April 24, 2019

String Interview Questions In Java

Q- What is string in java?

Q- Why string immutable in java?

Q- How many way to create string object in java?

Q- What is the use of intern() method of string in java? or What does intern() method do?

Q- What is String Pool?

Q- Is String thread safe in java?

Q- How many objects will be created in below code ?

  1. String str1 = new String("Hello Java");
  2. String str2 = new String("Hello Java");

Answer : Total 3 object.
Two objects created at line-1. code. one in heap and second in the string pool.
One object created at line-2. code. because java always create new object while using new keyword.
but as "Hello Java" is already present in string pool so it will reuse the same from string pool and don't create new on in string pool again.









Related Tutorial

No comments:

Post a Comment