[Java lista] ket alap kerdes
istvan.ketler at lhsystems.com
istvan.ketler at lhsystems.com
2007. Feb. 13., K, 13:06:20 CET
> A 2. esetében ez mindig is így volt. Ha leírod a forrásban
> ugyanazt a Stringet többször, akkor csak egyszer rakja le a
> memóriába, és arra mutat mindkét változó.
Lásd még az API doc-ban a String osztály intern() metódusát, illetve az ott hivatkozott Java Language Specification paragrafust:
API:
A pool of strings, initially empty, is maintained privately by the class String.
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
---
JLS:
* 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 by concatenation 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.
Üdvözlettel,
Iván
További információk a(z) Javalist levelezőlistáról