wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many strings are 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 "How many strings are created?" Watch "How many strings are created?" New topic

How many strings are created?

Sandeep Lodhia
Ranch Hand

Joined: Oct 09, 2000
Posts: 105
Please tell me how many strings are created.
String s=new String(" aa ").trim(..).substring(..).reolace(..);
( .. = valid arguments for corresponding methods)
Oliver Grass
Ranch Hand

Joined: Nov 02, 2000
Posts: 65
Girish P

Joined: Nov 11, 2000
Posts: 7
Answer should be 4.
Can anybody explain why 5 ?
Oliver Grass
Ranch Hand

Joined: Nov 02, 2000
Posts: 65
sorry for the short answer before...
I will give a try, you have to correct me if i'm wrong. And i don't want to confuse people...
1) new String(" aa ");
this is in fact the copy-constructor of string. So first a string -literal is created (" aa "), then a new String-Object is created. Now this is the point where i'm not that sure....
Is the String-literal (" aa ") treated as an String-Object or not???
I would vote for yes, and so this creates in fact 2 Strings...
2) trim().
Cause there are whitespaces before and after the "aa", trim() returns a new String...
3) same with substring(), returns a new String-Object
4) replace() also, returns a new String-Object...
So, if you take the the two from the first one, plus the 3 more, you get 5....
Hope that helps,
as always correct me if i'm wrong
John Wetherbie

Joined: Apr 05, 2000
Posts: 1449
If you refer to sections 3.10.5 & 4.3.3 of the Java Language Spec (softcopy HTML version, not sure if the book is organized the same way) it discusses String literals and the fact that they are references to instances of class String.
So it sure looks like 5 is the right answer (if I am understanding the JLS correctly).
You should have access to the JLS via the docs/index.html page of the JDK you downloaded.

The only reason for time is so that everything doesn't happen all at once.
- Buckaroo Banzai
shailesh sonavadekar
Ranch Hand

Joined: Oct 12, 2000
Posts: 1874
Sorry for raining the parade, but I must remind 'Girish P' that PROPER NAMES ARE NOW REQUIRED!!
Please Read the JavaRanch naming policy for more details.
Javaranch appreciates your cooperation to comply with the official naming policy.
Please rergister yourself with proper last name & help javaranch to maintain the decorum of the forum.

[This message has been edited by shailesh sonavadekar (edited November 21, 2000).]
asim wagan
Ranch Hand

Joined: Nov 14, 2000
Posts: 62
So, if the above discussion is not wrong it means this means the answer for following should be, 3:
Aru Ven
Ranch Hand

Joined: Sep 28, 2000
Posts: 199
Good Morning friends...
String s=new String(" aa ").trim(..).substring(..).replace(..);
Well I think it will create 4 string objects.
1. String s=new String(" aa ")
2. s.trim()
3. s.substring
4. s.replace
Can any one explain more clearly how it is 5.

bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Actually I think the answer is at most 5 and at least 3. As Oliver said, the first line creates 2 strings, which he provides a good explanation from above, and since " aa " has spacing, then the trim function will return a new string, so that gives you 3. But since we don't know the arguments of substring and replace, they may or may not return a new string. So you can have from 3 to 5 strings created here.
Just thought I would throw that out there.
Aparna Narayanan
Ranch Hand

Joined: Nov 07, 2000
Posts: 44
The answer is 4.
When you create a string by saying
new String(..), then only one string is created.
It is filled with the value given as argument to the constructor. If no value is given, the String object stores a value of null in it.
Three subsequent methods of String are called which return String objects, and hence 1+3=4 String objects are created.
Hope I am clear.
I agree. Here's the link: http://aspose.com/file-tools
subject: How many strings are created?