Constant Strings

Discussion in 'Java' started by Alexander Mueller, Mar 22, 2005.

  1. I am using several constant strings (for comparisons, ....) and am
    wondering whether javac is smart enough to notice that and creates one
    string object which is used in the entire code or whether I would be
    better off with a

    private static final String VAR="VALUE";

    and reference VAR. I think it is smart enough :), but really would like
    to have it confirmed.

    Alexander
     
    Alexander Mueller, Mar 22, 2005
    #1
    1. Advertisements

  2. Alexander Mueller sez:
    String literals present at compile time are automatically interned.
    Exactly what interning does in practice is the $15 question: in some
    of my tests a custom pool of unique strings showed better compression
    than interning -- for mostly duplicate strings (interning worked
    better for mostly unique strings).

    I'd recommend doing the "private static final" thing anyway --
    declaring your constants as members is a better programming style.

    Dima
     
    Dimitri Maziuk, Mar 22, 2005
    #2
    1. Advertisements

  3. Alexander Mueller wrote:

    Do your mean you are doing something like...


    if (aString.equals("VALUE") ) {
    }


    if so, then you aren't using a string constant, you are using a string
    literal.

    It is always better to use a proper constant like below. The code will
    be more maintainable and less error prone.

    The String class does optimize memory usage by creating a pool of
    strings. Take a look at
    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html
     
    Andrew McDonagh, Mar 22, 2005
    #3
  4. Well, that was the question. Whether javac generates separate instances
    or uses one general instance.

    Alexander
     
    Alexander Mueller, Mar 22, 2005
    #4
  5. So in theory it shouldnt matter, but practically its more "safe" to
    refer to a "global" variable.

    Thanks,
    Alexander
     
    Alexander Mueller, Mar 22, 2005
    #5
  6. Alexander Mueller

    Lisa Guest

    It should be simple to run your app with jdb and take a look.
     
    Lisa, Mar 22, 2005
    #6
  7. The Java Language Specification explicitly states that a conformant Java
    implementation will use the same String instance for all equal,
    compile-time constant Strings, including String literals, static field
    values, and String-valued expressions evaluated at compile time (e.g.
    concatenation of String literals). This involves work on the part of
    the compiler and the VM both.
     
    John C. Bollinger, Mar 22, 2005
    #7
  8. Alexander Mueller sez:
    In practice you only need to change it in one place if it needs
    to be changed, you can fetch it from properties file at startup
    later, etc.

    Dima
     
    Dimitri Maziuk, Mar 22, 2005
    #8
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.