In Java 7, sottoStringa di stringa viene modificato per:
/**
* Returns a new string that is a substring of this string. The
* substring begins with the character at the specified index and
* extends to the end of this string. <p>
* Examples:
* <blockquote><pre>
* "unhappy".substring(2) returns "happy"
* "Harbison".substring(3) returns "bison"
* "emptiness".substring(9) returns "" (an empty string)
* </pre></blockquote>
*
* @param beginIndex the beginning index, inclusive.
* @return the specified substring.
* @exception IndexOutOfBoundsException if
* <code>beginIndex</code> is negative or larger than the
* length of this <code>String</code> object.
*/
public String substring(int beginIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
int subLen = value.length - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
}
Quindi, ogni volta che si fanno SOTTOSTRINGA con beginIndex NON uguale a 0, abbiamo nuovo oggetto String.
fonte
2015-10-09 11:54:19
Questa non è tecnicamente una perdita di memoria, poiché l'array di caratteri è ancora referenziato e potrebbe essere raccolto in seguito quando tutte le stringhe che lo fanno riferimento vengono raccolte. Parte dell'array di caratteri potrebbe non essere più utilizzata, ma ciò non lo rende una perdita. – cdhowie
se si dispone di una stringa 100 ogni 100 MB e si dispone di una sottostringa (0,1) si tiene tecnicamente quel valore [] utilizzato nella classe String e mai nell'applicazione stringhe enormi sono idonee per la garbage collection –
miglior collegamento http://javarevisited.blogspot.com/2012/03/why-character-array-is-better-than.html – Premraj