Sagewire Logo

Does JAVA automatically intern strings?

10 Message(s) by 8 Author(s) originally posted in java programmer


From: pedro.checo Date:   Friday, October 26, 2007
Hi,

From 1.5 and on, does JAVA automatically intern string s?



During a recent team discussion, we thought we read somewhere that
this was added to 1.5 as default behaviour, but we weren't sure. By
automatically interning I mean that if you've String a = new
String("ABC") then

String str = new String("ABC")

is equivalent to

String str = a.intern();

Thus far, we've found nothing to say that this was added.

Thanks in advance for your input.


From: Eric Sosman Date:   Friday, October 26, 2007
wrote in message On 10/26/07 17:40,:
Hi,
From 1.5 and on, does JAVA automatically intern strings?
During a recent team discussion, we thought we read somewhere that
this was added to 1.5 as default behaviour, but we weren't sure. By
automatically interning I mean that if you've String a = new
String("ABC") then
String str = new String("ABC")
is equivalent to
String str = a.intern();
Thus far, we've found nothing to say that this was added.
Thanks in advance for your input.



Why not just try the experiment?

String s1 = "ABC";
String s2 = new String("ABC");
System.out.println(s1 == s2);

For extra amusement, you could check a few other things:

System.out.println(s1 == s1.intern());
System.out.println(s2 == s2.intern());

--
Eric.Sosman@xxxxxxxxxxx


From: pedro.checo Date:   Friday, October 26, 2007
wrote in message:
wrote in message On 10/26/07 17:40,:
> Hi,
From 1.5 and on, does JAVA automatically intern strings?
> During a recent team discussion, we thought we read somewhere that
> this was added to 1.5 as default behaviour, but we weren't sure. By
> automatically interning I mean that if you've String a = new
> String("ABC") then
> String str = new String("ABC")
> is equivalent to
> String str = a.intern();
> Thus far, we've found nothing to say that this was added.
> Thanks in advance for your input.
Why not just try the experiment?
String s1 = "ABC";
String s2 = new String("ABC");
System.out.println(s1 == s2);
For extra amusement, you could check a few other things:
System.out.println(s1 == s1.intern());
System.out.println(s2 == s2.intern());
--
Eric.Sos...@xxxxxxxxxxx Hide quoted text -
- Show quoted text -



Good point. I should've been more specific. If that isn't the
default behaviour, then is there a flag (or any other way) to make it
so? We'd have to change a legacy library in order to take advantage of
String.intern() and'd like to avoid making code changes unless
absolutely necessary.


From: Eric Sosman Date:   Friday, October 26, 2007
wrote in message On 10/26/07 18:02,:
wrote in message:
wrote in message On 10/26/07 17:40,:






Hi,

From 1.5 and on, does JAVA automatically intern strings?

During a recent team discussion, we thought we read somewhere that
this was added to 1.5 as default behaviour, but we weren't sure. By
automatically interning I mean that if you've String a = new
String("ABC") then

String str = new String("ABC")

is equivalent to

String str = a.intern();

Thus far, we've found nothing to say that this was added.

Thanks in advance for your input.

Why not just try the experiment?

String s1 = "ABC";
String s2 = new String("ABC");
System.out.println(s1 == s2);
Good point. I should've been more specific. If that isn't the
default behaviour, then is there a flag (or any other way) to make it
so? We'd have to change a legacy library in order to take advantage of
String.intern() and'd like to avoid making code changes unless
absolutely necessary.



I'm not a language lawyer, but I think the `new'
operator is *required* to produce a brand-new object
distinct from all existing objects. Check the JAVA
Language Specification if you want to find out for
sure.

... but what are these changes you contemplate,
and how do you propose to "take advantage" of interned
Strings? If all Strings were in fact interned so that
`s1.equals(s2) == (s1 == s2)' always held, you could
get away with using == instead of .equals(). But the
legacy library'd still get the right answer with
.equals(), just as it always did, and it wouldn't be
"absolutely necessary" to change it. So what's up?

--
Eric.Sosman@xxxxxxxxxxx


From: kohlerm Date:   Friday, October 26, 2007
Hi,

wrote in message:
wrote in message On 10/26/07 18:02,:
[snip]
sure.
... but what are these changes you contemplate,
and how do you propose to "take advantage" of interned
Strings? If all Strings were in fact interned so that
`s1.equals(s2) == (s1 == s2)' always held, you could
get away with using == instead of .equals(). But the
legacy library'd still get the right answer with
.equals(), just as it always did, and it wouldn't be
"absolutely necessary" to change it. So what's up?
--
Eric.Sos...@xxxxxxxxxxx Zitierten Text ausblenden -
- Zitierten Text anzeigen -



knowing when Strings are interned and when new copies of the
underlying char[] is very important to avoid consuming too much
memory .
Check my blog at https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/5095

Regards,
Markus


From: A. Bolmarcich Date:   Friday, October 26, 2007
wrote in message:
wrote in message On 10/26/07 17:40,:
Hi,

From 1.5 and on, does JAVA automatically intern strings?

During a recent team discussion, we thought we read somewhere that
this was added to 1.5 as default behaviour, but we weren't sure. By
automatically interning I mean that if you've String a = new
String("ABC") then

String str = new String("ABC")

is equivalent to

String str = a.intern();

Thus far, we've found nothing to say that this was added.

Thanks in advance for your input.
Why not just try the experiment?
String s1 = "ABC";
String s2 = new String("ABC");
System.out.println(s1 == s2);
For extra amusement, you could check a few other things:
System.out.println(s1 == s1.intern());
System.out.println(s2 == s2.intern());



For even extra amusement

System.out.println(("A"+"B"+"C") == s1);


From: Patricia Shanahan Date:   Friday, October 26, 2007
wrote in message:
Hi,
wrote in message:
wrote in message On 10/26/07 18:02,:
[snip]
sure.

... but what are these changes you contemplate,
and how do you propose to "take advantage" of interned
Strings? If all Strings were in fact interned so that
`s1.equals(s2) == (s1 == s2)' always held, you could
get away with using == instead of .equals(). But the
legacy library'd still get the right answer with
.equals(), just as it always did, and it wouldn't be
"absolutely necessary" to change it. So what's up?

--
Eric.Sos...@xxxxxxxxxxx Zitierten Text ausblenden -

- Zitierten Text anzeigen -
knowing when Strings are interned and when new copies of the
underlying char[] is very important to avoid consuming too much
memory.
Check my blog at https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/5095



Your blog seems to be about memory consumption and the sharing of the
underlying char[]. Perhaps you could explain the connection you seem to
see between that and intern?

Patricia


From: Lew Date:   Friday, October 26, 2007
wrote in message:
knowing when Strings are interned and when new copies of the
underlying char[] is very important to avoid consuming too much
memory.



Actually, it probably does not make a whole lot of difference except in corner
cases.

As a rule of thumb, the JVM and API (application programming interface)are very smart about managing String
literals, although they'll be essentially permanent. That should not sweat
your memory consumption unless you've a true crapload of literals.

That leaves String intermediate values and new-allocated instance s. API (application programming interface)
method s like String.substring() tend to be pretty smart about reusing existing
char [] backing arrays, but it's pretty hard, too, to predict how the
optimizer will handle situations. Inlining, loop unrolling and
common-subexpression refactoring are things it does that I can imagine
affecting String allocation, much less interning.

I suspect that most programs facing memory problems won't find interning to
be much of an issue or much help. I'm also pretty sure that there are times
when it'll. And there always come those times when every last drop counts.

My concern is that the real impact on performance of different idioms is so
very often counter-intuitive.

--
Lew


From: Roedy Green Date:   Friday, October 26, 2007
wrote in message, quoted
or indirectly quoted someone who said :

String str = new String("ABC")
is equivalent to
String str = a.intern();



nope. The very opposite. See http://mindprod.com/jgloss/intern.html
--
Roedy Green Canadian Mind Products
The JAVA Glossary
http://mindprod.com


From: Owen Jacobson Date:   Friday, October 26, 2007
wrote in message:
Hi,
>From 1.5 and on, does JAVA automatically intern strings?
During a recent team discussion, we thought we read somewhere that
this was added to 1.5 as default behaviour, but we weren't sure. By
automatically interning I mean that if you've String a =3D new
String("ABC") then
String str =3D new String("ABC")
is equivalent to
String str =3D a.intern();
Thus far, we've found nothing to say that this was added.



It wasn't.

You've to read between the lines a bit, but the JLS forbids this
optimization if its effects'd be detectable. Follow along, if you
will:

1=2E 15.21.3 states that "At run time, the result of =3D=3D is true if the
operand values are both null or both refer to the same object or
array; otherwise, the result is false."

2=2E 4.3.1 states that "An object is a class instance or an array." and
that "A class instance is explicitly created by a class instance
creation expression." The reference for that second statement covers
expressions of the form 'new Type (Arguments)', which implicitly
includes 'new String ("Some literal")'.

Those two statements combine to imply that

String a =3D new String ("foo");
String b =3D new String ("foo");

boolean same =3D (a =3D=3D b);

will always result in 'same' containing false, eg., that the two
objects aren't the same instance.

3=2E 3.10.5 states, "String literals-or, more generally, strings that
are the values of constant expressions (=A715.28)-are "interned" so as
to share unique instances, using the method String.intern," which
does not cover new expressions, and the documentation for String.intern
follows this with

"for any two strings s and t, s.intern() =3D=3D t.intern() is true if
and only
if s.equals(t) is true."

which implies that were strings automatically interned such that 'new
String ("foo")' evaluated to an interned instance of "foo", then

String a =3D new String ("foo");
String b =3D new String ("foo");

boolean same =3D (a.intern () =3D=3D b.intern ());

would result in 'same' being set to true, since a.equals(b) and the
strings would've been interned.



Next Message: verisign security


Blogs related to Does JAVA automatically intern strings?

Pack
... 2005 malpack.com sixpacksite package javax.servlet does not exist hiram walker co pack enviroventally friendly hiking packs what is the blue packet used for in a urine drug screen jpromoter.full.pack.v.2.5.zip tbng package and java ...

[fm-news] Newsletter for Wednesday, October 24th 2007
About: Sleep is a Perl-inspired scripting language for the Java platform. Sleep has its own library of functions for performing common tasks as well as being able to utilize the Java class library. Sleep includes closures, ...

C/C++ Faqs Collection 1
(Microsoft) C is not an object-oriented language, but limited object-oriented programming can be done in C. Name some major differences between C++ and Java. C++ has pointers; Java does not. Java is platform-independent; C++ is not. ...

Java History
When no references to an object remain, the Java garbage collector automatically deletes the unreachable object, freeing memory and preventing a memory leak. Memory leaks may still occur if a programmer's code holds a reference to an ...

Kreepy Krauly
Kreepy Krauly Pool Cleaner journals on rural health care financing jordans.com java int string jim adkins jimmy eat world jobs aberdeen md kreepy krauly pentair john locke vs thomas hobbes jenkins enterprise joss stone feet photos ...

Signal
titanic fired destress signals at signal cartridges dm13 perl qt signal load string signalco as heat sensor 'wolo industrial horn and signal' power & signal infloat and out float pcm signalling signal generator bk precision 2005 ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional