Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


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

How to reduce flicking in animation?

3 Answers   TCS,


What are different data structures in java?

0 Answers  


Is singleton thread safe in java?

0 Answers  


How to call static method?

4 Answers   Epoch,


What is the string function?

0 Answers  


What is a substring of a string?

0 Answers  


what is difference between Interface and abstract class

2 Answers   GCPL,


using equals method overriding which objects are compared?i.e same class objects or other class objects?Explain me.

4 Answers  


Is alive and join method in java?

0 Answers  


How do you check whether the list is empty or not in java?

0 Answers  


Can we call the Thread.sleep in Synchyronozed block?

5 Answers   Logica CMG,


what is the difference between String s="hello"; and String s=new String("hello");?

3 Answers  


Categories