How many objects are created when we create String class
object using new operator?
Answer Posted / eknath wagadre
No!!!!!!!!!!!!!!!!!!!!
According to the Question it will creating only One object
in the Heap only..........
if we are using String str = new String("abc"); for this
statement only one object is creating in the heap only....
if we are using the code like
String str = "abc";
String str = new String("abc");
In this case only one object is creating in the heap.bcz of
in first line obviously one object is creating in the pool
and assigning the reference(S) to abc. now come in second
line it's creating object in heap but jvm will checking for
reference(s) bcz both object reference is same so now (s) is
pointing to abc which is in heap and pool object is
collected by GC. now we have only one object is heap.
if we are using the code like
String str ="abc";
String str1 = "abc";
String str2 =new String("abc");
Then only two object is creating one in string content pool
and another one is in heap.
Thank's
Eknath
| Is This Answer Correct ? | 16 Yes | 6 No |
Post New Answer View All Answers
Explain polymorphism citing an example.
Explain when noclassdeffounderror will be raised ?
How do you define a variable?
What is default size of arraylist in java?
What is string manipulation?
What do you mean by ternary operator in java?
What is singleton class example?
Can you use this() and super() both in a constructor?
What is boolean example?
What is difference between path and classpath?
Explain runtime exceptions?
Why string is immutable or final in java
What are different data types?
State some situations where exceptions may arise in java?
What are different types of references?