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
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.
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
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..
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).
i think answer is 4. for explanation, please refer following link
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?
Joined: Apr 23, 2006
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???
Joined: Jun 04, 2007
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.
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?
Joined: Jun 04, 2007
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??
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...