String, StringBuilder and StringBuffer - Interview Questions
If you are not a Medium member then you can use below friendly link to read the complete story.
Friendly Link so that everyone can read my article.
Hi Reader.
Ashay Nayak here.
Before we move to the interview questions, let’s understand the complete story of Strings in java. You will be able to answer interview questions by yourself.
See code below:
Integer obj1 = new Integer(2);
Integer obj2 = new Integer(2);
Q. How many objects get created in the above code?
Ans: Two objects. Obviously. The “new” keyword creates an object. We are using the “new” keyword in lines one and two. So, two objects get created in heap memory. Remember this.
Just FYI, All objects in java get stored in heap memory.
String in java is an object. So when you write String str = “Ashay”, it means ‘str’ is referring an object having the value “Ashay”. Here, the string object resides in the string pool. But, an object should be in heap memory. String pool is a part of heap memory only. It’s just we have given a section of heap memory dedicated to string objects only.