How many objects are created when we create String class
object using new operator?

Answers were Sorted based on User's Feedback



How many objects are created when we create String class object using new operator?..

Answer / koneti

yes, two Objects are created one Object is string constent
pool and second one is in heap memory

Is This Answer Correct ?    40 Yes 6 No

How many objects are created when we create String class object using new operator?..

Answer / guest

Two object will create.

Is This Answer Correct ?    30 Yes 8 No

How many objects are created when we create String class object using new operator?..

Answer / mahesh

yes two objects will be created when you use new operator
for a String. The reason for this is at first it creates an
object in HEAP and then verifies in the String Constant
Pooling if it is not available then creates the new one in
pool. so totally two object will be created.

Is This Answer Correct ?    22 Yes 5 No

How many objects are created when we create String class object using new operator?..

Answer / 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

How many objects are created when we create String class object using new operator?..

Answer / naveen

Yes,two Objects will be created, First in Constant Pool and
second Object in Heap area of the Memory

Is This Answer Correct ?    4 Yes 3 No

How many objects are created when we create String class object using new operator?..

Answer / rana

Answer is 2

String s = new String ("ABC"); // creates new object in heap & also adds entry in string literal pool.

String s1 = s.intern(); // will copy the string from pool
System.out.println(s1);



@Ravi -
System.out.println(s1.hashCode()==s2.hashCode() );

will return same because of String generates its hashCode based on the characters it has. there is a formula for hashCode generation on string


s[0]*31^n-1 + s[1]*31^n-2 ..... + s[n-1]

s[0] - 1st characters ascii value
n - length of the string

Is This Answer Correct ?    0 Yes 0 No

How many objects are created when we create String class object using new operator?..

Answer / rana

Answer is 2

String s = new String ("ABC"); // creates new obj in heap and adds entry in literal pool
String s1 = s.intern(); // fetches the existing object's value.
System.out.println(s1);

@Ravi
String hashCode has generic mechanism as below.

s[0]*31^n-1 + s[1]*31^n-2 ....+s[n-1]

n - length of string
s[0] - ascii value of the character

Is This Answer Correct ?    0 Yes 0 No

How many objects are created when we create String class object using new operator?..

Answer / shalini

using new operator, we can create same object 'n' no of
times of the same string class coz jvm would never verify
for the existing object wenever we create the object using
new operator

Is This Answer Correct ?    7 Yes 9 No

How many objects are created when we create String class object using new operator?..

Answer / ravi

Eknath Wagadre is correct, Check this programme.

String s1 = "this is string";
String s2 = new String("this is string");

System.out.format("S1: %d, S2:%d \n",s1.hashCode(),s2.hashCode() );
System.out.println(s1.hashCode()==s2.hashCode() );

The both s1 and s2 have same hashCode, means only one object created.

Is This Answer Correct ?    0 Yes 5 No

Post New Answer

More Core Java Interview Questions

What is a void return type?

6 Answers  


What is locale in java?

0 Answers  


What is class??

0 Answers   Tech Mahindra,


What is the purpose of using java.lang.class class?

0 Answers  


Why is the type for real numbers called double?

0 Answers  






Explain the importance of throwable class and its methods?

0 Answers  


Is ruby built on java?

0 Answers  


What is listnode in java?

0 Answers  


Why does the integer quotient -0/3 yield 0, but the double quotient -0.0/3.0 yields – 0.0?

0 Answers  


Why are data types important?

0 Answers  


What is a boolean output?

0 Answers  


Difference between a Canvas and a Scroll Pane?.

1 Answers   NIIT,


Categories