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.
khushhal yadav
Ranch Hand
Joined: Jun 20, 2007
Posts: 242
posted
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
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
posted
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
posted
Hi Atul,
KB means the SCJP book by Kathy Sierra & Bert Bates
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3057
posted
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
posted
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: 3057
posted
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
posted
Thanks
AtulKumar Gaur
Ranch Hand
Joined: Jun 24, 2007
Posts: 40
posted
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
posted
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
posted
hi all,
i think answer is 4. for explanation, please refer following link
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
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?
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 11293
posted
Howdy "pearl",
Welcome to the JavaRanch...
You may not have noticed that we have a policy on screen names here at the ranch. It must consist of a first name, a space, and a valid last name.
Unfortunately, your screen name does not seem to conform with this policy. Please take a moment to change it.
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
posted
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
posted
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??
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
posted
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...
Suresh Koutam
Gitesh Ramchandani
Ranch Hand
Joined: Feb 28, 2007
Posts: 274
posted
Excellent Explanation and it was really an Eye opener