Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many strings are created?

 
Sandeep Lodhia
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell me how many strings are created.
String s=new String(" aa ").trim(..).substring(..).reolace(..);
( .. = valid arguments for corresponding methods)
Sandeep
 
Oliver Grass
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
5
 
Girish P
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer should be 4.
Can anybody explain why 5 ?
 
Oliver Grass
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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
cheers
Oliver
 
John Wetherbie
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oliver,
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.
John
 
shailesh sonavadekar
Ranch Hand
Posts: 1874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

YOUR FRIENDLY BARTENDER
SHAILESH.
[This message has been edited by shailesh sonavadekar (edited November 21, 2000).]
 
asim wagan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI!
So, if the above discussion is not wrong it means this means the answer for following should be, 3:
"one"+2;
 
Aru Ven
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Aruna
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.
Regds,
Aparna
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic