In the declaration of strings, String s1="abc"; String s2="xyz"; String s3=new String("abcd");totally how many objects will be created?

Showing Answers 1 - 75 of 133 Answers

Levey

  • Aug 22nd, 2006
 

Three String objects will creates.

  Was this answer useful?  Yes

arunesh

  • Aug 23rd, 2006
 

answer is 1(one)

  Was this answer useful?  Yes

Antony

  • Aug 23rd, 2006
 

Only one Object will be create

  Was this answer useful?  Yes

harsh123

  • Aug 24th, 2006
 

according this question , only one obj will be create , other two string is not a obj these are only reference ,

thanks and regards

harsh nigam

Sauveer Acharya

  • Aug 24th, 2006
 

In the given 3 examples, only 1 object will be created. The remaining couple of them are merely references.

  Was this answer useful?  Yes

Dinesh singh

  • Aug 24th, 2006
 

In this case, three objects will be created, even in place of "abcd" you write "abc" then also three objects will be created.No doubt...

regards,

dinesh singh 

Kuldeep Sharma

  • Aug 25th, 2006
 

According to me , There atleast one object will be created which is third one, rest depends on the String pool if first two strings already available in the String pool then no new object will be created only reference will be assigned  otherwise new objects will be created.

KUSHDEV

  • Aug 25th, 2006
 

THREE OBJ WILL BE CREATED

ramesh

  • Aug 28th, 2006
 

Hi I agree with Kuldeep Sharma

His explanation is more appropriate

  Was this answer useful?  Yes

Mohit

  • Aug 29th, 2006
 

Hi,I think 4 objects will be created one for s1 and s2 each and 2 for s3.The string lietrals in s1 and s2 are after all String objects. Hence they are created in the String constant pool.For s3, a String object "abcd " will be created and one more string object will be created which goes in the String constant-pool.

Shaik Nowshad ali

  • Sep 4th, 2006
 

If you really want know how many objects are getting created use reflect API .

  Was this answer useful?  Yes

ashutosh chnadra

  • Sep 5th, 2006
 

Three object will be created in this case ...because every time when we assign value in the string it will create object at that time ..

baskar

  • Sep 6th, 2006
 

only one obj created

  Was this answer useful?  Yes

karthikeyan

  • Sep 7th, 2006
 

s1  - 1 object

s2 - 1 object

String("abcd") - 1 object

s3 - 1 object

totaly , 4 objects will be created.

  Was this answer useful?  Yes

Dikesh Mishra

  • Sep 8th, 2006
 

Hi to all.

According to the above question only one objects we are getting and remaing two are the variable declarations

that is s3 is the object we are getting

  Was this answer useful?  Yes

naresh kumar murali

  • Sep 10th, 2006
 

i think what you said was exactly right.''coz considering strings,if we have two strings with the same name then only one object is created.the first object reference is used for second object.however one object will be created for string object(i.e)String str=new String("abcd");so object str will be created.

  Was this answer useful?  Yes

leela

  • Sep 14th, 2006
 

only one object will be created

  Was this answer useful?  Yes

Jigar

  • Sep 14th, 2006
 

3 objects will be creared but if it isString s1="abc";String s2="abc";String s3=new String("abc");than only 2 objects will be created coz s1 will create first string object than s2 will look for string abc on heap it will find abc so it will assign s2 to s1's objectwhere as there is no question of s3 coz its creating complete new string object using NEWthanx & regds,Jigar+91 9886998771

  Was this answer useful?  Yes

Satish Reddy

  • Sep 17th, 2006
 

All the three wil be created. I've tested a sample code aswell.

  Was this answer useful?  Yes

Abdulaziz

  • Sep 22nd, 2006
 

According to me this statement will create three objects, because in java if you create a string like String s ="abc" it will also be considered as a object.So it will create 3 object.

    Please comment on this.

  Was this answer useful?  Yes

Lakshman

  • Oct 5th, 2006
 

Hi ,

Refrences can be made only to already existing Objects. For example reference can be like this  s1="abcd"; s1="abcdef" and son on .....

But not like String s1="abcd"; String s1="abcdef";

In the below case 3 objects will be created. It is simple as you see it is declaration

String s1="abc";
String s2="xyz";
String s3=new String("abcd");

String s1="abc" is equalant to new String("abc").

Both String and new String() will invoke the Constructor  String() in java.lang.String.

  Was this answer useful?  Yes

Naveen Muniraja

  • Nov 16th, 2006
 

hi people, In the above scenario only one object will be created when we use String literals...... The case is a bit different......these values will be stored in what is known as a Constant Pool in the JVM.......the object created will be stored in the heap



Thanks and Regards

  Was this answer useful?  Yes

chinnu

  • Nov 18th, 2006
 

hi,compulsory created three string objects.if in the string pool has object with references then we cant give that same reference to another object.if do the same it will give the compilation error.thats why if above program has executed ,it creates the compulsory three objects.

  Was this answer useful?  Yes

Satish Bakde

  • Nov 24th, 2006
 

For the given scenario jvm will create 3 different objects. When we call

String s = new String( "someString" );

it always creats it new instance. But when a call is made like..

String s1 = "someOtherString";

jvm checkes whether is there already an instance of the String object "someOtherString" in the stack segment of allocated memory. If it is found its reference is returned otherwise a new object is created.

  Was this answer useful?  Yes

Here comes the expert reply from SJCP

In this case three objects are created with no doubt. If the first two i.e. s1 and s2 are references with same value "abc" then if u compare (s1 == s2) it should result true but it results false. where as if u compare (s1.equals(s2) | s2.equals(s3) | s3.equals(s1) ) it results true. Hence the concept is verified.

  Was this answer useful?  Yes

sonia

  • Mar 2nd, 2007
 

yes, ur right , 4 objects will be created , first two case two objects will be created

and for the one new is used 2 objects will be created , one n string pool and one in memory reference pool .

  Was this answer useful?  Yes

Sujaniw

  • Jul 6th, 2007
 

4 String objects are created.
s1,s2 and s3 are 3 String objects.
And copy of the s3 will go to the String literal pool(abcd literal).

So finally 4 objects are created.

  Was this answer useful?  Yes

pranju245

  • May 24th, 2008
 

I think there are three objects are created named as s1,s2 and s3 in which s1 and s2 are static string variables and s3 is dynamic variable.

  Was this answer useful?  Yes

zizou

  • May 26th, 2008
 

Hi

Actually here we have 3 references and one object.

the 2 first references are s1 and s2)
the 3 reference is s3 which is pointing to the object created (new string "abcd");

  Was this answer useful?  Yes

raama_k

  • Oct 1st, 2009
 

Hello folks,

String someName = "someString";

or

String someName = new String("someString");

these two statements are one and some.... the first one is just a short form. That is just a privilege JAVA has given us.

Coming to the issue... there are three objects created. Check this code....

class howManyObjectsCreated{
    public static void main(String args[]){
        String s1 = "abc";
        String s2 = "xyz";
        String s3 = new String("pqrs");
       
        System.out.println("hashCode of s1: "+s1.hashCode());
        System.out.println("hashCode of s2: "+s2.hashCode());
        System.out.println("hashCode of s3: "+s3.hashCode());   
    }
}

I hope all of you know that hashCode is unique for each object. And, the above programs prints three different hash codes. So, there are three objects created.

Answer is :THREE

Here 4 objects will be created.S1 and s2 will create 2 object and s3 will create 2 object .

String s3; //It will create one object

After that it will object to another object which new String("abcd") will create.

So total 4 object will be create.

  Was this answer useful?  Yes

sapanindia

  • Aug 16th, 2010
 

One objects will be created.
1.b'coz we know that  generly using new operator object will be created
so, String s3=new String("abcd");// one obj created only


  Was this answer useful?  Yes

4 objects will be created
1st >>> referenced by s1 in string constant pool memory(because of "abc")
2nd >>> referenced by s2 in string constant pool memory(because of "xyz")
3rd >>> referenced by s3 in string constant pool memory(because of "abcd")
4th >>> as there is a new keyword used so a 4th object will be created in heap
memory


  Was this answer useful?  Yes

shra1.dsk

  • May 13th, 2011
 

Hi,

If there are 4 objects created, then the variable s3 would point to which memory? string pool memory or heap memory?

  Was this answer useful?  Yes

3 objects will be created.

The first statement ie

String s1  = "abc";

An object is created in the String pool and its reference is assigned to s1.

Similarly for the next statement ie

String s2 =  "xyz";

an object will be created in the String pool and its reference will be assigned to s2.

And for the third statement ie;

String s3 = new String("abcd");

a new object is created in the heap and its reference will be given to s3.

So finally we will get 3 objects.

  Was this answer useful?  Yes

ambinu

  • Jun 14th, 2011
 

Since String is a immutable class, It creates object every time initialized with new value.
s3 is also a similar method of initializing a string object. so, 3 objects will be created.

  Was this answer useful?  Yes

aiswarya

  • Aug 1st, 2011
 

3

  Was this answer useful?  Yes

Hi,

According this syntax three object will be created .Because String is a class and when object is created.

memory allocation is different for each object..
in this case ..

String s1="ABC";
String s2= "abc";
String s3 = "Abc";

because JVM checks the value is same then it create three object but all object point at same address

  Was this answer useful?  Yes

Seeker

  • Aug 3rd, 2011
 

4 objects

  Was this answer useful?  Yes

krishna

  • Aug 3rd, 2011
 

only one object is created that is S3..

  Was this answer useful?  Yes

ThulasiRam

  • Aug 3rd, 2011
 

My answer is 2 objects because in compile time there is 2 objects &run time the "new" keyword creates the obj for string so its necessary to count as one object .
its my opinion as well as answer .please send reply as soon its correct or not,if wrong please send solution with explanation

  Was this answer useful?  Yes

ONLY ONE OBJECT WILL BE CREATED.

String s1 IS JUST THE REFERENCE
AND SO String s2 is just the reference.

the String 3 is the object as u used String s3=new String("abcd");

any classname preceded with new keyword will only creates an object.

Anchit MIttal

  Was this answer useful?  Yes

bapuji

  • Aug 13th, 2011
 

three objects created

  Was this answer useful?  Yes

Aishvarya

  • Aug 14th, 2011
 

no there will be in total 3 objects created..

  Was this answer useful?  Yes

palwindersingh

  • Aug 16th, 2011
 

the one object is created

In The above Program Only One Object Will Be Created....
Because..

In the Second Declaration Of Second Line which is [ String s3 = new String("abcd"); ]
The Keyword new is used and as we know that new keyword is used to create a object....

Thank You.....

  Was this answer useful?  Yes

nik7i

  • Aug 28th, 2011
 

There are 3 objects created
According to syntax

string s1= new string("abcd")
syntax create an object of string

hence there are 3 object

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions