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 Number of String objects 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 "Number of String objects" Watch "Number of String objects" New topic
Author

Number of String objects

Karthick Ravi
Greenhorn

Joined: Feb 27, 2008
Posts: 13
Guys, got this question which I fail to understand. Hellppppppp....


Q How many String objects are created in the method?

public String returnString() {
String str = “Hello”;
str = str + “World”;
str = str.toLowerCase();
str = str.substring(2, 5);
return str.toString();
}


My understanding:
String objects are immutable. Hence, any manipulation of an existing object is done by creating a new String object.

Therefore. My answer is that the number of objects created are 6. The list is

1. "Hello"
2. "World"
3. str + “World”
4. str.toLowerCase();
5. str.substring(2, 5);
6. str.toString();


Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Karthick Ravi wrote:Guys, got this question in a bank ...

In a bank, what exactly do you mean by that? Please quote your sources.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

I am not sure. I see the String Source Code.

i dont think str.toLowerCase() and str.toString create Objects. so the object creations are 4
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

According to me 3 objects are created in pool and 3 on the heap.


Enjoy, Chander
SCJP 5, Oracle Certified PL/SQL Developer
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

chander shivdasani wrote:According to me 3 objects are created in pool


String Constant Pool is nothing but Collection Of references which is pointed to an Object in Heap
Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317

My understanding is that the second operand "World" (from x + y) is not created, because we have here a call to the concat method str.concat("World"), where "World" is just a parameter, which is not a string object (EDIT: here is an error. look at my or Tomasz post !!!) (lookup the method concat in string class).
The toLowerCase()-method creates a new object with the new keyword, like substring(), whereas toString()-method just returns the string itself(return this;)

String Constant Pool is nothing but Collection Of references which is pointed to an Object in Heap

Are you sure about this? I thought there are string objects in the pool as well.

cheers
Bob


SCJP 6 - SCJD - SCWCD 5 - SCBCD 5
JavaEnterpriseEditionFaq - TomcatFaq
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 813

chander shivdasani wrote:According to me 3 objects are created in pool and 3 on the heap.


I think it is 5 in total....But the answer is 3


Regards, Sud.
SCJP 5 ScjpFAQ JLS
Tom Kowalski
Ranch Hand

Joined: Feb 17, 2009
Posts: 72
But why the answer is 3? Where do you found this answer and why do you think it is true?

In K&B there was quite simillar example


It is also written that String which were made are: 'spring', 'summer', 'spring summer'. So the beginng is quite similar to the example which you have given. So I think we should agree that in this two lines, there are three objects.

In my opinion:


SCJP, SCWCD, OCUP
Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
EDIT: Tomasz changed his post during my upload/writing.

Tomasz Stachlewski wrote:
In my opinion:

Ok. The string parameter of str.concat("World") or str + "World" creates a String object "World". I was wrong there. So we have 5 objects.
But str.toString() doesn't create a new object. The object referenced by str already exist.

cheers
Bob
David Marco
Ranch Hand

Joined: Feb 23, 2009
Posts: 44
--


SCJP 6
Duc Vo
Ranch Hand

Joined: Nov 20, 2008
Posts: 254
** REMOVED **
David Marco
Ranch Hand

Joined: Feb 23, 2009
Posts: 44
Duc, I thought the same two post above, but I had to rectify quickly and erase all my post. The first line:

creates a new String object as says the Java API specification:
All string literals in Java programs, such as "abc", are implemented as instances of this class.

Furthermore the string literals are linked to the string literal pool (which contains only references to objects, not objects). The String creation on the heap is mandatory. I think the given answer is wrong.
Duc Vo
Ranch Hand

Joined: Nov 20, 2008
Posts: 254
** removed **
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Duc Vo wrote:
David, the first three lines will be optimised into
String str = "HelloWorld";
Hence JVM won't create String literals "Hello" nor "World".


Disagree .
Karthick Ravi
Greenhorn

Joined: Feb 27, 2008
Posts: 13

In K&B there was quite simillar example



This was actually brought up in a study group apparently which is a modification of the above question. They also don't have a definite answer. The answer was given as 3 earlier as my birthday present to leave me confused for the rest of the day.
But it has brought some rare insights.
Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
I don't think we can assume any compiler optimizations for the sake of exam. Otherwise, results can be different depending on which compiler user chooses. So work through the examples assuming details given only in the Java Language Specification.

Having said that and assuming no compiler optimizations, answer for this question is 5 and as follows. The list of objects/values shows the snapshot of heap after a corresponding statement is executed. (lost) indicates that object doesn't have a valid reference.


Duc Vo
Ranch Hand

Joined: Nov 20, 2008
Posts: 254
seetharaman venkatasamy wrote:
Disagree .


I tend to agree with you now after doing some research. So I'll remove my previous posts:

Anyway, here is what the bytecode looks like after compiling the above code:



So there are "Hello", "World", "HelloWorld", "helloworld", and "llo".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Number of String objects
 
Similar Threads
How many String objects will be created ?
Singular string from plural noun
Remove last character from a string
How many objects are Garbage collected?
String and StringBuffer