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


Please Help Members By Posting Answers For Below Questions

Which package is imported by default?

827


Does java runtime require a license?

764


Explain about wait() method?

767


How do you input a string in java?

731


What type of variable is error flag?

816


What are the advantages and disadvantages of reference counting in garbage collection?

767


What is string :: npos?

766


What are scriptlets?

806


What are the differences between wait() and sleep()?

728


Can a main method be overloaded?

762


Describe method overriding

799


What is final?

793


What are the advantages of defining packages in java?

724


How are multiple inheritances done in Java?

832


Justify your answer that you can't define a method inside another method in java, if you can then how?

793