Document directory
- Do you know the string substring function in Java?
Do you know the string substring function in Java?
The substring function in Java is a frequently used function used to intercept the substring of the current string. Its definition is as follows:
public final class String{ public String substring(int beginIndex); public String substring(int beginIndex, int endIndex);}
The usage and declaration are very simple, but do you know the details?
Let's take a look at the implementation of substring:
1 public String substring(int beginIndex, int endIndex) { 2 if (beginIndex < 0) { 3 throw new StringIndexOutOfBoundsException(beginIndex); 4 } 5 if (endIndex > count) { 6 throw new StringIndexOutOfBoundsException(endIndex); 7 } 8 if (beginIndex > endIndex) { 9 throw new StringIndexOutOfBoundsException(endIndex - beginIndex);10 }11 return ((beginIndex == 0) && (endIndex == count)) ? this :12 new String(offset + beginIndex, endIndex - beginIndex, value);13 }
In row 3, a new string is returned, and three parameters are input: Offset, count, and the value (char []) of the original string object.
Continue to look at the constructor of the string of row 12th:
String(int offset, int count, char value[]) { this.value = value; this.offset = offset; this.count = count;}
This is the internal method of a package. It is not public. Note that it directly captures the passed char [] and defines the start position and length.
I understand that the original intention of his design isTo save memory, the new string still holds the reference of the old string value, but only redefines the start position and length..
The field Declaration of the string class can be further clarified:
public final class String{ /** The value is used for character storage. */ private final char value[]; /** The offset is the first index of the storage that is used. */ private final int offset; /** The count is the number of characters in the String. */ private final int count; /** Cache the hash code for the string */ private int hash; // Default to 0}
---------------------------------------------
Under the gorgeous split line is my use scenario:
Read a very large text file row by row. Each row has a large length and extracts a small part of the file (using the substring function). About 10 words are extracted from each row, A large number of lines (the entire text may contain dozens of MB or more ).
After reading the file, I understand that the memory consumption is not very high, but it is totally beyond my imagination that the memory consumption is very large, and even outofmemory.
My code:
List<String> results = new ArrayList<String>();InputStream stream = new FileInputStream(filePath);BufferedReader bufferredReader = new BufferedReader( new InputStreamReader(stream));while (true) { String line = bufferredReader.readLine();
if (line == null) {
break;
}
results.add(line.substring(10, 20));}
Why? After viewing the string substring function and Google from multiple parties, I finally understood it.
In the past, the newly constructed string still grabbed the text of each line, but adjusted the offset and length. We didn't understand that we only grabbed a small text. The original long text was recycled by GC.
What should I do? You only need to change the following sentence:
List<String> results = new ArrayList<String>();InputStream stream = new FileInputStream(filePath);BufferedReader bufferredReader = new BufferedReader( new InputStreamReader(stream));while (true) { String line = bufferredReader.readLine(); if (line == null) { break; } results.add(new String(line.substring(10, 20)));}
Construct a new string and pass in the string. This has nothing to do with the original string. Therefore, when using substring, you must pay attention to the use scenario.
Finally, we will remind you that the same is true for the substring returned by the string split function.