This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
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
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
The only reason for time is so that everything doesn't happen all at once.
- Buckaroo Banzai
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).]
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.
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.
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