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 the purpose of lambda expressions?

0 Answers  


String is a immutable objects . it means that string does not change........... But it will be chang......... { String s="kapil"; String s1="raj"; String s=s1; then print(.......) The String has been changed .. how it is possible and why its called immutable objects

7 Answers  


How will you print number in reverse (descending) order in BST.

0 Answers   GrapeCity,


What is a super class and how super class be called?

3 Answers  


What is a cup of java?

0 Answers  






Does printwriter create a file?

0 Answers  


how to identify duplicate values in arraylist

2 Answers   TCS,


What is enhanced loop in java?

0 Answers  


What is a final class ?

0 Answers  


Explain about sets?

0 Answers  


What is the primitive type short?

0 Answers  


What are the different conditional statements?

0 Answers  


Categories