This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.



Only 44 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Best way to declare a String  RSS feed

 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String s = new String("shekar");
String s = "shekar";
What is the best way to declare a String?
I have been asked this question by my friend to which I replied the following:
IN the first declaration there are two objects created one is the "shekar" string object and other is a empty string object and once that line completes execution the reference s will be assigned the string"shekar"

But in the second declaration only one string will be declared and hence the second declaration is better. is that true? i remember to have read the same somewhere?
any help is appreciated.thanks.
 
Howard Kushner
author
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Chandra,
The basic difference is this:

So the second option save memory as well as being a bit more efficient.
Now consider this code:

Explanation:
s1 and s3 are refences to the same object.
s2 and s4 are references to newly created object.
Hope this helps. Please post again if you need a bit more of an explanation.
Best Regards,
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Performance aside, whenever I see the first style I tend to assume that the author didn't really understand Java very well. There's no need for an explicit constructor here; it just looks silly. There are only two cases I know of where the new String(String) constructor is useful for anything:
  • Creating hypothetical garbage collection problems which are not affected by the String intern pool
  • Creating a copy of a string which uses only as much memory as is necessary for the String itselt. This is useful if you create a String as a substring() of a much larger String - the new String shares the same backing array as the old one, which may take up more memory than you want. If you wish to GC the original String while retaining a smaller substring in memory, then making a copy using new String(String) can be helpful
  • Personally I recommend not using Strings at all for GC problems; they just confuse people. As for the second, the few times it's come up, I include a comment in the code so that other coders know I'm not just being foolish.
    [ November 03, 2003: Message edited by: Jim Yingst ]
     
    Ilja Preuss
    author
    Sheriff
    Posts: 14112
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Jim Yingst:
    Careting a copy of a string which uses only as much memry as is necessary for the String itselt. This is useful if you create a String as a substing() of a much larger String - the new String shares the same backing array as the old one, which may take up more memory than you want. If you wish to GC the original String while retaining a smaller substring in memory, then making a copy using new String(String) can be helpful

    Are you sure? I'd suspect that new String(String) would share the array, too - I currently can't look at the source code, though...
     
    Chandra Bairi
    Ranch Hand
    Posts: 152
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hello thanks for the reply but can you please explain the last two replies once again. i could not understand a bit of it. can you mind giving examples. thanks once again
     
    Jim Yingst
    Wanderer
    Sheriff
    Posts: 18671
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Are you sure? I'd suspect that new String(String) would share the array, too - I currently can't look at the source code, though...
    Well I'm sure that it was true for a JDK I was using a couple years ago - probably 1.2. And it's true for J2SDK 1.4.2, just checked the source. However unforutnately it's not actually guaranteed in the API, so I guess we can't assume it will always be true. It did allow me to significantly reduce the memory overhead in one application though. I was pleased to discover that String(String) actually did have a use for once, since otherwise it's just found in poorly-written GC problems.
    FWIW, the 1.4.2 code does reuse the internal array if it's already the exact size needed. But if there's any opportunity to reduce the size, even by just one byte, a new array is allocated and populated for use by the new String.
     
    Andres Gonzalez
    Ranch Hand
    Posts: 1561
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Since this is a performance forum, I'd like to ask: if I'm getting a value from a property file (string value), is it recommended to declare it as final?

    does that declaration make a lot of difference when I declared as final?
     
    Ernest Friedman-Hill
    author and iconoclast
    Sheriff
    Posts: 24215
    37
    Chrome Eclipse IDE Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    does that declaration make a lot of difference when I declared as final?

    Performance-wise? No, none. "final" often helps make code a bit easier to understand, though; and most programmers should worry more about how to write easy-to-read code than micro-optimized code.
     
    Ilja Preuss
    author
    Sheriff
    Posts: 14112
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Jim Yingst:
    Are you sure? I'd suspect that new String(String) would share the array, too - I currently can't look at the source code, though...
    Well I'm sure that it was true for a JDK I was using a couple years ago - probably 1.2. And it's true for J2SDK 1.4.2, just checked the source. However unforutnately it's not actually guaranteed in the API, so I guess we can't assume it will always be true. It did allow me to significantly reduce the memory overhead in one application though. I was pleased to discover that String(String) actually did have a use for once, since otherwise it's just found in poorly-written GC problems.
    FWIW, the 1.4.2 code does reuse the internal array if it's already the exact size needed. But if there's any opportunity to reduce the size, even by just one byte, a new array is allocated and populated for use by the new String.

    I just take a look at the code and you are right of course.
    Thanks for the clarification!
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!