• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How many objects are created ?

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Search for "spring", "summer", "winter". This questions is frequently asked.
It is meant as object count prior to println line.
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 242
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Superb explanation khusshal yadav!
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI
Suresh what do you mean by KB?
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks once again Khushhal for correcting me.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ur explanation is outstanding khusshal
 
AtulKumar Gaur
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Atul,

KB means the SCJP book by Kathy Sierra & Bert Bates
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please go through this excellent article by Corey McGlone which speaks about "Strings, Literally".

This is very much useful
 
khushhal yadav
Ranch Hand
Posts: 242
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
AtulKumar Gaur
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

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

String Literals
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A similar topic was discussed some days back.

link for that is How Many String Object are created - 19 June
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent Explanation and it was really an Eye opener
 
I brought this back from the farm where they grow the tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic