जावा 7 में, स्ट्रिंग के स्ट्रिंग को संशोधित किया गया है:
/**
* 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);
}
इसलिए, हर आप beginIndex 0 के बराबर नहीं के साथ-स्ट्रिंग है, हम नए स्ट्रिंग वस्तु है।
स्रोत
2015-10-09 11:54:19
यह तकनीकी रूप से एक स्मृति रिसाव नहीं है, क्योंकि चरित्र सरणी अभी भी संदर्भित है और इसे बाद में एकत्र किया जा सकता है जब सभी स्ट्रिंग्स जो इसे संदर्भित करते हैं। चरित्र सरणी का हिस्सा अब और उपयोग नहीं किया जा सकता है, लेकिन यह इसे रिसाव नहीं बनाता है। – cdhowie
यदि आपके पास प्रत्येक 100 एमबी 100 बड़ी स्ट्रिंग है और आपके पास एक सबस्ट्रिंग (0,1) है तो आप तकनीकी रूप से उस मूल्य को पकड़ रहे हैं [] स्ट्रिंग क्लास में उपयोग किया जाता है और कभी भी आपके एप्लिकेशन में भारी स्ट्रिंग कचरा संग्रह के लिए योग्य नहीं हैं –
सर्वोत्तम लिंक http://javarevisited.blogspot.com/2012/03/why-character-array-is-better-than.html – Premraj