How many objects are created when we create String class
object using new operator?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
What are the restrictions imposed on method overriding?
Is there any use of an abstract class which has no methods and no attributes?
Is it safe to install java on my computer?
hi am an engineering student and my next plan is for ms in either in us or australia i got my passport but i dont know anything bout visa can u give brief idea 1)How to get prepared for visa and 2)How to apply for top universities and 3)How to pay the fee and so on These all are basic questions plz give me a clear idea
What are the different types of collections in java?
how does the run() method in runnable work? : Java thread
where can we get jAVA project orientation traing in HYDERABD
Can you tell me range of byte?
What is the method to declare member of a class static?
Is string a datatype?
interface X{ void m1(); void m2(); } class Child2 extends Object implements X { public void m1(){ System.out.println("Child2 M1"); } public void m2(){ System.out.println("Child2 M2"); } } public class ParentChildInfterfaceDemo { public static void main(String[] args){ X x = new Child2(); X x1 = new Child2(); x.m1(); x.m2(); x.equals(x1); } } Will the above code work? If yes? Can you please explain why the code x.equals(x1) will work as the equals method is called on interface reference vaiable?
what is sendredirect?