File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how many String object have been created? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "how many String object have been created?" Watch "how many String object have been created?" New topic
Author

how many String object have been created?

Beny Na
Ranch Hand

Joined: May 26, 2004
Posts: 159
Hi..

i still confused with question number 3 from kathy bert books page 391.
the question is:
-----------
String x = new String("xyz");
y = "abc";
x = x + y;

how many String objects have been created?
answer : 4

how does it work to get 4???
is the statement String x = new String("xyz"); create a string in the pool and in the heap(i read it from prev post, but not very sure)


any explanation or reference to any tutorial about it would be great for me.


thanks



SCJP
Lukas Alamar
Ranch Hand

Joined: Mar 17, 2004
Posts: 68
String x = new String("xyz");
- creates 1 String object in normal non-pool memory
- creates 1 object in the String constant pool

y = "abc";
- creates 1 object in the String constant pool

x = x + y;
- creates 1 new object adding the content from x and y

that sums up to 4




SCJP 1.4
Beny Na
Ranch Hand

Joined: May 26, 2004
Posts: 159
thanks for the reply...

String x = new String("xyz");
- creates 1 String object in normal non-pool memory
- creates 1 object in the String constant pool

in case we "never" create a string object using :
x = "abc"; which mean we always create a String object using:
String s = new String("abc"); (one Str obj in pool and one in heap)
String d = new String("def"); (one Str obj in pool and one in heap)
String e = new String("ghi"); (one Str obj in pool and one in heap)
and more...
up to 100 String with "different character", so there will be 200 String object that has been created(100 in the heap and 100 in the string pool)?
is this correct, if yes, then what is the function of creating in the pool and in the heap?


thanks
Lukas Alamar
Ranch Hand

Joined: Mar 17, 2004
Posts: 68
yes, that is correct...
the point in having a String constant pool is that is memeory efficient:

String s = new String("abc"); (one Str obj in pool and one in heap)
if at any place in your program you code:
String t = "abc";
t will refer to the object already in the pool without having to create a new object.
Beny Na
Ranch Hand

Joined: May 26, 2004
Posts: 159
what i was asking about from my prev posting is that there will be wasted effort to create such a string in pool and heap(only in my example case)..
in another case creating a string object in heap and pool will be efficient thought, but if there are many strings in the pool then it can effect the performance because we need to compare if such a string already define/exist in constrant string pool.


thanks
siva prakash
Ranch Hand

Joined: Aug 24, 2004
Posts: 80
Hi all,

iam an J2ME developer , i found this article interesting and enuthesatic about knowing the following things. Any reply is apreciated

what are the advantages & disadvantages of
1) creating String in heap only
2) creating string in String pool only
3) creating string on both heap and string pool

4) how can create string on heap only
5) how can create string on pool only


Thanks & Regards,

siva
Louie van Bommel
Ranch Hand

Joined: Aug 17, 2004
Posts: 76
what are the advantages & disadvantages of
1) creating String in heap only
2) creating string in String pool only
3) creating string on both heap and string pool

4) how can create string on heap only
5) how can create string on pool only


1. Most programming does this. For example using + or += does it, or using new.
2. If you are coding using literal Strings, it puts that String in the pool. You can't avoid it.
3. No advantage That's just crazy!
4. String s = "hello". (use a literal).
5. use "new" in the string creation expression.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by siva prakash:
iam an J2ME developer , i found this article interesting and enuthesatic about knowing the following things. Any reply is apreciated


You may want to check out this article.


SCJP Tipline, etc.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Zain,

The String Literal Pool is really just a group of references to String objects. All objects are created on the heap, regardless of whether or not they are immutable. However, in order to have a reference to an object, that object must exist. The key difference between String literals and other Strings is when they are instantiated.

In the original example, we have this code:



Looking at this code, you should quickly be able to spot two String literals, "xyz" and "abc". Any time you see a String literal, you should know that a String object is going to be created. But when?

Well, unlike other objects, String literals are created when the class is loaded. When a class is loaded, any String literals are created on the heap and references to those Strings are placed into the String literal pool.

So, based on that, we know that we have two objects created before we've even executed the code. Now, let's go through the code and see what happens.



This line creates a brand new String object that is equivalent to the one that is already referenced from the String literal pool. However, since we used the "new" operator, we create a brand new String - we do not re-use the one that is referenced from the pool. We're now up to three objects.



This line performs a String concatenation. Well, we know that Strings are immutable so, in order to put two Strings together, we need to create a brand new String. That's where the fourth object comes from - it's the result of concatenation of two other String objects.

I hope that helps,
Corey
Mohit Agarwal
Ranch Hand

Joined: Mar 30, 2004
Posts: 88
Hi,
Thanks to all for bringing such a nice discussion and specially to Corey.

I just want to know that if the scenario is the following:

String x="abc";//1 obj. created
String s=new String("abc");//No. of objects created?

Will the number be one or two?
I put my money on one.

Am i rite or Wrong.
Pls. Clarify.

Would Be SCJP.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
In all, two objects will be created. One will be created when the class is loaded - that object represents the literal "abc". When this code is executed, we'll create a second String object based on this line:



The String literal "abc" won't cause a new object to be created (because it already has been), but using the keyword "new" forces the JVM to create a new String object that has the same value as the one referenced from the String literal pool.

I hope that helps,
Corey
Mohit Agarwal
Ranch Hand

Joined: Mar 30, 2004
Posts: 88
Hi,

Thanks fo r your reply it indeed helped me.

Mohit Agarwal.
Would Be SCJP.


" The will to win is worthless if you do not have the will to prepare."
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8875
    
    5
Awesome job Corey!


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how many String object have been created?