aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Stings: how many objects created? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Stings: how many objects created?" Watch "Stings: how many objects created?" New topic
Author

Stings: how many objects created?

Prateek Rawal
Ranch Hand

Joined: Dec 19, 2009
Posts: 90
Just suppose there are no objects in the String pool:
Let us find out how many objects are created and where?

Case 1:


(1) object is created with value "abc" in the pool and it is referred by s.

[Again suppose there are no objects in the pool]
Case 2:


(2) objects are created, one in the pool with value "abc" and other in the non-pool memory with value "abc" only.


[Again suppose there are no objects in the pool]
Case 3:


Now how many objects are created?
I guess(i'm not sure) (2) objects are created,
By LINE 01, a new object is created on pool with value abc,
Now when we write LINE 02, it first search whether there exist a String object with value "abc" in the pool, if it does(as in the present case), it gets the referece of the String object in the pool(here s) and then s is passes to the constructor of String, and a new object is created in the non-pool memory with value "abc"

Am i correct? Correct me if i'm wrong.....

PS: I don't have any idea to confirm my understanding or interpretation, so i find it better to raise it as a topic on javaranch.........

Waiting for enlightenment....
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Correct. I can sure this....


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Hi, go through this link, at the bottom of the thread, there is a good example related with String Literal Pool.
Prateek Rawal
Ranch Hand

Joined: Dec 19, 2009
Posts: 90
Excellent help Abhimaram!

And the best part was, wht code you put up asking how many objects were created,

I solved it myself and got the answer as 13,hehe........

Thanks very much.

But only one thing i didn't get, the one with toString()

Look at the following:


Here, one apple literal pool object is create, and a builder object is created with value apple, but what about
s1.toString()...what does it do???
Does it return the apple object on the pool as Ankit told???
Prateek Rawal
Ranch Hand

Joined: Dec 19, 2009
Posts: 90
Also i have two doubts:

1) Is String Pool different from Heap? Or Pool + Non-Pool = Heap?

2) They said, there would be no GC questions on Strings in SCJP, however, can there be questions on Strings about how many objects are created? and how many of them are in pool and how many are in Non-Pool Memory?? it seems quite probable, isn't it?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Prateek Rawal wrote:Excellent help Abhimaram!


Somebody changed my name!~
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Prateek Rawal wrote:Also i have two doubts:

1) Is String Pool different from Heap? Or Pool + Non-Pool = Heap?


String Literal Pool is a part of the heap!

Prateek Rawal wrote:
2) They said, there would be no GC questions on Strings in SCJP, however, can there be questions on Strings about how many objects are created? and how many of them are in pool and how many are in Non-Pool Memory?? it seems quite probable, isn't it?


No.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Prateek Rawal wrote:
Here, one apple literal pool object is create, and a builder object is created with value apple, but what about
s1.toString()...what does it do???
Does it return the apple object on the pool as Ankit told???


There is 'space' to StringBuilder object in the String Literal Pool, Check here and here
Prateek Rawal
Ranch Hand

Joined: Dec 19, 2009
Posts: 90
You said

String Literal Pool is a part of the heap!


In the link you gave me earlier, Ankit said:

The toString method of a String object never creates a new String object, neither in the heap nor in the constant pool, it just returns the String object itself i.e. this.


Also in the link you gave me just now i.e. this, in the diagram String pool is shown different from Heap, i guess both are different, just check it out once again......


Hey, those links you gave me, i'm not able to interpret and locate the reference to StringBuilder having a space in the pool, just describe me here.....

Also, Ankit said:
The toString method of a String object never creates a new String object, neither in the heap nor in the constant pool, it just returns the String object itself i.e. this.

Going by this: it can be interpreted that as far as this code is concerned

it creates apple object on the pool and the StringBuilder object on the non-pool, then s1.toString() doesn't create a new object neither in the heap nor in the pool, but returns the String object associated with the StringBuilder object i.e. in present case- "apple" object.

What say?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

can there be questions on Strings about how many objects are created? and how many of them are in pool and how many are in Non-Pool Memory?? it seems quite probable, isn't it?

You can definitely face a question which asks how many String objects are created, but you won't be asked separately how many will be created in the pool or non-pool.
in the diagram String pool is shown different from Heap

The String literal pool is a part of the heap.

@Prateek: you are calling the toString method of StringBuilder class not String class. What I said was about toString method of String class...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Prateek Rawal
Ranch Hand

Joined: Dec 19, 2009
Posts: 90
Then what does toString() of StringBuilder do, please answer with reference to this code:


Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
Returns a string representing the data in this sequence. A new String object is allocated and initialized to contain the character sequence currently represented by this object. This String is then returned. Subsequent changes to this sequence do not affect the contents of the String.


quote source:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuilder.html

EDIT:
I appologize, on second look I am not 100% sure what's happening.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Two String objects will be created here, one in the pool and one created by the toString method of StringBuilder class...
Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
Funny
Prateek Rawal
Ranch Hand

Joined: Dec 19, 2009
Posts: 90
where does the string object created by toString() method of StringBuilder get created?

In the non-pool memory i suppose, as one String object with same value is already there in the pool
Correct me if i'm wrong
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Prateek Rawal wrote:where does the string object created by toString() method of StringBuilder get created?

In the non-pool memory i suppose, as one String object with same value is already there in the pool
Correct me if i'm wrong



Strings are immutable because String literals with same content share the same storage in the string common pool. Modifying the content of one String directly may cause adverse side-effects to other Strings sharing the same storage. JDK provides a StringBuffer class (in package java.lang) to support mutable strings. A StringBuffer object is just like any ordinary object, which are stored in the heap and not shared, and therefore, can be modified without causing adverse side-effect to other objects.


StringBuilder and StringBuffer are almost same except the synchronization, so will create a StringBuilder object in the heap not in the String Literal Pool, But will create a String object in the String Literal Pool necause there is no ABC literal in that pool.

Check the toString() method API of the StringBuilder.
Prateek Rawal
Ranch Hand

Joined: Dec 19, 2009
Posts: 90
IMO you are slightly going wrong,

see,


will first see if the string pool has a "abc" string literal object,
1) if it does, then the reference to that literal object is retrieved and then passed in the constructor of StringBuilder to create a new StringBuilder object with same value "abc"
2)if it does not have a "abc" string literal object, then a new literal object is created in the pool and then its reference is fetched and passed in the constructor of StringBuilder to create a new StringBuilder object with same value "abc"

And now Ankit is saying,
creates a new String object(with value "abc") may be in the non-pool memory as one exists in the pool already(but i asked him and he is yet to confirm it)

What say?

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Prateek Rawal wrote:IMO you are slightly going wrong,


So, waiting for somebody....
Prateek Rawal
Ranch Hand

Joined: Dec 19, 2009
Posts: 90
Hey, i felt what you suggested is not correct, it's an opinion, thats it!
Don't take it otherwise and before we come to conclusion, we should always get opinions of as many as we can, right?

I wrote down the code and explanation(what i feel is correct) just so that you can ponder over it and rethink and suggest something...
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Abimaran Kugathasan wrote: will create a StringBuilder object in the heap not in the String Literal Pool, But will create a String object in the String Literal Pool necause there is no ABC literal in that pool.

The toString method of StringBuilder class clearly states that it creates new String object. Every String object that is created is not added to the String literal pool. The String literal pool only contains String literals of programs. The statement will create a String in the pool which will have the value "ABC" and also a StringBuilder object in the heap. Then s.toString(); will create another String object with the value "ABC" in the heap (not in the pool)...
Prateek Rawal
Ranch Hand

Joined: Dec 19, 2009
Posts: 90
which means i was thinking correct....

Thanks Ankit!!!

Thanks Abimaran
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18997
    
  40


Just pointing out... that although this is really interesting, it is going deep into the implementation territory, and hence, subject to change ... and way out of scope for the SCJP exam.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
 
subject: Stings: how many objects created?