Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2004

Tip: Looking for answers? Try searching our database.

initialize String

Thread view: 
Petterson Mikael - 21 Sep 2004 07:49 GMT
Hi,

Found in the code that I am maintaining:

private String currentOperation;
currentOperation = new String("deleteGroup()");

Is it not better to write:

private String currentOperation;
currentOperation = "deleteGroup()";

I am not sure of the difference.
Any ideas which to prefer.

//Mikael
Aki \ - 21 Sep 2004 08:00 GMT
> Hi,
>
[quoted text clipped - 7 lines]
> private String currentOperation;
> currentOperation = "deleteGroup()";

No. Here you don't actually initialize the currentOperation String.
(Missing the "new String(...)" part).
You can't do

private String currentOperation = new String();
currentOperation = "deleteGroup()";

either, because Strings are static, so you can't modify a String after
you've initialized it.

If you want to modify the contents of currentOperation, you've got to
change it to a StringBuffer.

Signature

-Aki "Sus" Laukkanen

Stefan Schulz - 21 Sep 2004 09:08 GMT
> Hi,
>
[quoted text clipped - 10 lines]
> I am not sure of the difference.
> Any ideas which to prefer.

Why not just use

private String currentOperation = "deleteGroup()";

Signature

Using Opera's revolutionary e-mail client: http://www.opera.com/m2/

Aki \ - 21 Sep 2004 09:51 GMT
> Why not just use
>
> private String currentOperation = "deleteGroup()";

Isn't it supposed to be done like this:

private String currentOperation = new String("deleteGroup()");

?

Signature

-Aki "Sus" Laukkanen
"No Akia nyt on niin helppo raadella - senkun vaan raatelee."

Stefan Schulz - 21 Sep 2004 10:02 GMT
On Tue, 21 Sep 2004 11:51:13 +0300, Aki "Sus" Laukkanen  
<aki.laukkanenREMOVETHIS@helsinki.fi> wrote:

>> Why not just use
>>  private String currentOperation = "deleteGroup()";
>
> Isn't it supposed to be done like this:
>
> private String currentOperation = new String("deleteGroup()");

Why? You can use a String literal in any place you can use a String  
Object. After all, the Literal
is nothing but a fancy way of writing an Object. (you could also use
private String currentOperation = new String(new char[]{'d', 'e', 'l',  
'e', 't', 'e', 'G', 'r', 'o', 'u', 'p',
'(', ')'}); if you really insist, but this has no advantage over using a  
literal.

Signature

Using Opera's revolutionary e-mail client: http://www.opera.com/m2/

Tor Iver Wilhelmsen - 21 Sep 2004 10:08 GMT
> private String currentOperation = new String("deleteGroup()");

No, that creates an unnecessary String object. String literals are
String objects on their own.
Aki \ - 21 Sep 2004 10:15 GMT
>>private String currentOperation = new String("deleteGroup()");
>
> No, that creates an unnecessary String object. String literals are
> String objects on their own.

Ah.
Wasn't aware of that. My bad. :)

Signature

-Aki "Sus" Laukkanen

Tony Morris - 22 Sep 2004 07:43 GMT
> Hi,
>
[quoted text clipped - 7 lines]
> private String currentOperation;
> currentOperation = "deleteGroup()";

Yes, it is better - since you are making use of the "String literal pool"
(an abstract concept).
It will give a minor performance increase and more readable code.

I tend to get annoyed at verbose code that does nothing other than hinder
readability and give a performance hit (regardless of how negligible it
might be) simply due to misinformation of the language that the developer is
using. I do, however, have tolerance for those who are learning, making
mistakes and gaining a better understanding as a result (if this is the
case, notify the author of the code that you know something that might be in
their interest).

I suggest using the JLS as a reference, specifically the section on "String
literals" - I'd look it up, but I'm sure that you are more than capable.

Good luck.

Signature

Tony Morris
http://xdweb.net/~dibblego/

Thomas G. Marshall - 25 Sep 2004 14:50 GMT
Tony Morris coughed up:
>> Hi,
>>
[quoted text clipped - 13 lines]
>
> I tend to get annoyed at verbose code that does nothing [...]

I'd like to point out that the following:

   (str1 comes from StringBuffer)
   String str2 = new String(str1);

is *actually important*.  There are times when the underlying array within
the String (it's actually in there, go look), can grow out of control even
though the string part is fairly small.

This was best described by Jon Skeet a long time ago:

http://groups.google.com/groups?selm=MPG.160abca06e214209989e47%40mrmog.peramon.com

<QUOTE from Jon Skeet, 13-Sep-2001>
   Consider:

   StringBuffer largeBuffer = new StringBuffer (100000);
   largeBuffer.append ("small text");

   String largeString = largeBuffer.toString();
   map.put (largeString, value); // Whoops, 200K of memory gone

   // This copies the contents rather than sharing the buffer,
   // so we end up with a much smaller memory footprint
   String smallString = new String (largeString);
   map.put (smallString, value);

   It's the only worthwhile use of the String constructor which takes a
   String that I've seen. It's *very* useful when reading large numbers of
   strings in from files using a BufferedReader, as the BufferedReader
   starts off with an 80 character buffer each time. In the case of reading
   a dictionary, you could be wasting vast amounts of memory in unfilled
   buffers.
</QUOTE>

...[rip]...

Signature

Onedoctortoanother:"Ifthisismyrectalthermometer,wherethehell'smypen???"



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.