aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many objects are created ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How many objects are created ?" Watch "How many objects are created ?" New topic
Author

How many objects are created ?

suresh koutam
Ranch Hand

Joined: Dec 29, 2004
Posts: 30
Guys...
How many objects are created here...if carefully observed we think it is 8 but look at the last println statement....s1 + " " + s2....wont this create two new objects and make the total 10 objects??? (KB says there are 8 objects created)
Please help

String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat("fall ");
s2.concat(s1);
s1 += "winter ";
System.out.println(s1 + " " + s2);



Suresh Koutam


SCJP aspirant<br />SCWCD aspirant
John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
Search for "spring", "summer", "winter". This questions is frequently asked.
It is meant as object count prior to println line.
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
Well!!But i am not exactly sure .
According to me there are 8 objects getting created,the explanation for this is below.

1)String s1 = "spring ";
2)String s2 = s1 + "summer ";
3)s1.concat("fall ");
4)s2.concat(s1);
5)s1 += "winter ";
6)System.out.println(s1 + " " + s2);

in line 1 first object S1=Spring
inline 2 second object S2=Spring+Summer;
in line 3 third object (as concat method is used)S1=Spring fall;
in line 4 fourth obj (as concat method is used)S2=Spring summer Spring fall

in line 5 S1+="winter"

S1=S1+winter;(so i think here two objects getting created)
so now all together there are 6 objects
and in the System.out.println statement two more objects get created
so total number of objects is 8.
khushhal yadav
Ranch Hand

Joined: Jun 20, 2007
Posts: 242

Sorry dhwani mathur

There are ten objects..

1 object ("spring") created in line 1
2 objects ("springsummer" and "summer") in line 2
2 objects ("springfall" and "fall") in line 3
1 object ("springsummerspring") in line 4
2 objects ("springwinter" and "winter") in line 5
2 objects ("spring "(with space at end) and "spring springsummer") in line 6

So in total there are ten objects

Regards..


rgrds,
Khushhal
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Superb explanation khusshal yadav!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
AtulKumar Gaur
Ranch Hand

Joined: Jun 24, 2007
Posts: 40
HI
Suresh what do you mean by KB?
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
Thanks once again Khushhal for correcting me.
pearl
Greenhorn

Joined: Jun 25, 2007
Posts: 1
ur explanation is outstanding khusshal
AtulKumar Gaur
Ranch Hand

Joined: Jun 24, 2007
Posts: 40
Great Explanation Mr. Yadav
But again its a question of rely how can we rely on such type of book .It is just like the misleading the concepts.
AtulKumar Gaur
Ranch Hand

Joined: Jun 24, 2007
Posts: 40
Hi
Can anybody tell me any way that we can count the object in such type of case so that the guys who is preparing of SCJP do not get confussed.
Ishita Saha
Ranch Hand

Joined: May 30, 2007
Posts: 39
Hi Atul,

KB means the SCJP book by Kathy Sierra & Bert Bates
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Please go through this excellent article by Corey McGlone which speaks about "Strings, Literally".

This is very much useful
khushhal yadav
Ranch Hand

Joined: Jun 20, 2007
Posts: 242

Hi AtulKumar Gaur

As such there is no way on which you can rely to count the objects..
The only thing is to concentrate yourself on the thing you are doing..
That you won't get confused so often also..
As you have all the things absolutely clear in front of you..

Regards..
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344


Originally posted by Atulkumar Gaur:
HI
Suresh what do you mean by KB?


K -> Kathy Seirra
B -> Bert Bates


They are the authors of the well-known, famous book for SCJP! Of course, they are the founders of Javaranch too! Bert Bates is the active sheriff here in Javaranch forums.

HtH.
AtulKumar Gaur
Ranch Hand

Joined: Jun 24, 2007
Posts: 40
Thanks
AtulKumar Gaur
Ranch Hand

Joined: Jun 24, 2007
Posts: 40
Thanks
I am very much aware about well known writter Kathy and Bert .
I was just only confussed about the short form KB that is used.
Now its clear to me that KB-- Kethy and Bert
Thanks again
AtulKumar Gaur
Ranch Hand

Joined: Jun 24, 2007
Posts: 40
Thanks
I am very much aware about well known writter Kathy and Bert .
I was just only confussed about the short form KB that is used.
Now its clear to me that KB-- Kethy and Bert
Thanks again
Amrit Kashyap
Ranch Hand

Joined: Apr 23, 2006
Posts: 44
hi all,

i think answer is 4. for explanation, please refer following link

String Literals
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by khushhal yadav:
2 objects ("spring "(with space at end) and "spring springsummer") in line 6

So in total there are ten objects

This may not be correct. It depends on how the compiler implements the String concatenation.
Amrit Kashyap
Ranch Hand

Joined: Apr 23, 2006
Posts: 44
A similar topic was discussed some days back.

link for that is How Many String Object are created - 19 June
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Originally posted by khushhal yadav:
2 objects ("spring "(with space at end) and "spring springsummer") in line 6


It might be right that there are two String objects involved in the following line:
6)System.out.println(s1 + " " + s2);

but things work a little differently behind the scenes:

So, there are two String objects involved: the " " literal and the String object returned by toString() method. However, as Manfred suggested, don't forget that string literals are created when the class that 'declares' them is loaded. That's why it is necessary to pay attention to the phrasing of the question:
1) how many string objects created when method is invoked (this one shouldn't take into account the "string literals")
vs.
2) how many objects created before the execution of line xx (in this case it could be fine to include the literals).


SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
well!!
After reading the link shown by

Devashish Roy


i think answer is 4. for explanation, please refer following link

String Literals


i am bit confused to how the answer can be 4?
first i was expecting the answer to be 8 later with the explanation given
by khushhal i came to understood the answer to be 10....but this time....it is becoming hectic
to what is answer?
Amrit Kashyap
Ranch Hand

Joined: Apr 23, 2006
Posts: 44
I am also waiting for the right answer. This will help me in solving question on SCJP exam. So which way is right. Do i consider String literels as objects or not???

Do anybody can answer this???

thanks
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Hi Devashish,

as Sergio has said. It depends on the phrasing of the question.

The answer is eight, since it doesn't matter that the objects for the string literals are already created before the code executes.


In this case the answer is three, since the objects for the string literals are created before the method is invoked.
Mohit Jain
Ranch Hand

Joined: Jun 04, 2007
Posts: 74
Hey Manfred

Does this mean that all string literals (written with double quotes) are allocated when class loads first time, even if they are declared within a method which is never called in a program??


SCJP 5.0, SCWCD in progress
Siva Selvan
Greenhorn

Joined: Jun 26, 2007
Posts: 1
6)System.out.println(s1 + " " + s2);
-----------------------------------------

I have doubt in line 6. How many object(s) are created in this line?
2 or 3
Becoz
At first " " -- > one String object is created, say x
Next s1 + x --> new String object ,say y
Then y + s2 --> new String object , say z
Hence 3 objects created in Total [my understanding]!
Is it correct?
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by Mohit Jain:
Does this mean that all string literals (written with double quotes) are allocated when class loads first time, even if they are declared within a method which is never called in a program??


Yes. Have a look at Strings, Literally.
suresh koutam
Ranch Hand

Joined: Dec 29, 2004
Posts: 30
Thanks guys for the replies....
this is some thing that we all miss....when they ask about the number of objects created in the code....
Thanks for clarification and lets look out for these questions...



Suresh Koutam
Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
Excellent Explanation and it was really an Eye opener
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How many objects are created ?