Constant Strings

A

Alexander Mueller

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
 
D

Dimitri Maziuk

Alexander Mueller sez:
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.

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
 
A

Andrew McDonagh

Alexander Mueller wrote:

I am using several constant strings (for comparisons, ....) and am

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
 
A

Alexander Mueller

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

Well, that was the question. Whether javac generates separate instances
or uses one general instance.

Alexander
 
A

Alexander Mueller

Dimitri said:
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

So in theory it shouldnt matter, but practically its more "safe" to
refer to a "global" variable.

Thanks,
Alexander
 
L

Lisa

Alexander Mueller said:
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

It should be simple to run your app with jdb and take a look.
 
J

John C. Bollinger

Alexander said:
Well, that was the question. Whether javac generates separate instances
or uses one general instance.

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.
 
D

Dimitri Maziuk

Alexander Mueller sez:
So in theory it shouldnt matter, but practically its more "safe" to
refer to a "global" variable.

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top