[MUD-Dev] [TECH] String Classes, Memory Management, and Fragmentation
Jay Carlson
nop at mitre.org
Fri Jul 20 09:54:15 CEST 2001
"Kwon Ekstrom" <justice at softhome.net> writes:
> Please note that java interprets anything between quotes as a new
> String automatically. thus "bleh" is equal to new String("bleh"),
> except that in the latter you actually create 2 Strings.
No, that's not true. Quoting the Java language spec, section 3.10.5
http://www.javasoft.com/docs/books/jls/second_edition/html/lexical.doc.html#229653
| A string literal always refers to the same instance (§4.3.1) of
| class String.
and in fact:
| String literals---or, more generally, strings that are the
| values of constant expressions (§15.28)---are "interned" so as to
| share unique instances, using the method String.intern.
Aside from trying to second-guess performance, this is relevant for
the correctness of programs that use reference equality. The spec
goes on to enumerate exactly when you can depend on the ==-ness of
String literals that are .equal():
-- snip --
Literal strings within the same class (§8) in the same package
(§7) represent references to the same String object (§4.3.1).
Literal strings within different classes in the same package
represent references to the same String object. Literal strings
within different classes in different packages likewise represent
references to the same String object. Strings computed by
constant expressions (§15.28) are computed at compile time and
then treated as if they were literals. Strings computed at run
time are newly created and therefore distinct. The result of
explicitly interning a computed string is the same string as any
pre-existing literal string with the same contents.
-- snip --
I believe this was not true in the distant past, but for any modern
Java system it's true; the same text is in the first edition of the
JLS, which was a Java 1.0 document.
Looking at the 1.3 Javadoc for String(String s), it appears to
guarantee that (new String("bleh")) is not == to any other String.
Dunno if that's always been true, not worth it to do the
archaeology.
People interested in cute little languages should check out Lua.
http:www.lua.org http://www.tecgraf.puc-rio.br/lua/
it feels like it could be a nice mud language. One thing you won't
see from the documentation is that the implementation compiles to
around 80k of x86 code for both the core language and the base
library...
Jay
_______________________________________________
MUD-Dev mailing list
MUD-Dev at kanga.nu
https://www.kanga.nu/lists/listinfo/mud-dev
More information about the mud-dev-archive
mailing list