Use Arrays.copyOfRange:

public static <T> T[] copyOfRange(T[] original,
                                  int from,
                                  int to)

Copies the specified range of the specified array into a new array. The initial index of the range (from) must lie between zero and original.length, inclusive. The value at original[from] is placed into the initial element of the copy (unless from == original.length or from == to). Values from subsequent elements in the original array are placed into subsequent elements in the copy. The final index of the range (to), which must be greater than or equal to from, may be greater than original.length, in which case null is placed in all elements of the copy whose index is greater than or equal to original.length - from. The length of the returned array will be to - from.

The resulting array is of exactly the same class as the original array.

In your case:

String[] grp = Arrays.copyOfRange(elements, i, i + n);
Answer from NPE on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ substring-in-java
Java String substring() Method - GeeksforGeeks
April 11, 2025 - Explanation: In the above code example, we use the substring(int beginIndex) method and specify the start index which is 8 and it didn't specified the end index. So it will make substring from end of the given string and we get the substring as "Geeks". ... Example 2: Java program to extract a substring from specified start index to specified end index using substring(int beginIndex, int endIndex).
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_string_substring.asp
Java String substring() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... The substring() method returns a substring from the string.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ extracting-a-substring-as-an-array-of-characters-in-java
Extracting a substring as an array of characters in Java
To extract a substring as an array of characters in Java, use the getChars() method. Letโ€™s say the following is our string and character array. String str = "World is not enough!"; char[] ch
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ strings in java โ€บ substring in java
Java String substring()
In JDK 7 and newer versions, substring() no longer counts the number of characters in the character array it creates, as it did in versions prior to JDK 6 inclusive, but simply creates a new array in memory (heap) and refers to it. Here is an example: String x = "CodeGymIsTheBest"; String y = x.substring (2,6); String z = x.substring (0,3); So, in JDK 7 and later, objects y and z created as a result of the substring() method applied to object x will refer to two newly created arrays (on the heap) - {d,e, G, y} for y and {C, o} for z.
Published ย  July 23, 2024
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 486735 โ€บ java โ€บ fast-substring-search-Array-strings
How to do a fast substring search on an Array of strings? (Java in General forum at Coderanch)
Searching the needle using Pattern.matches(".*needle*.*"). Found 1,400,000 needles in 149,735 milliseconds. Searching the needle using pattern.matcher(hayStack[i]).matches(). Found 1,400,000 needles in 34,379 milliseconds. Hope this is useful. Matt PS: I wanted to attach the source code I've used. Unfortunately .java, .zip, .tar and .gz are not valid extensions.
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 7 โ€บ docs โ€บ api โ€บ java โ€บ lang โ€บ String.html
String (Java Platform SE 7 )
A new String object is created, representing the substring of this string that begins with the character at index k and ends with the character at index m-that is, the result of this.substring(k, m+1). This method may be used to trim whitespace (as defined above) from the beginning and end ...
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 29382983 โ€บ getting-a-substring-of-an-array-in-java
Getting a substring of an array in java - Stack Overflow
// use StringBuffer to acquire reverse of text StringBuffer buffer = new StringBuffer(strTest.substring(2, 4)); // it will get '21' of '1221' String revStrTest = buffer.reverse().toString(); // from '21' to '12' ... // from array of test int[] test = { 1, 2, 2, 1}; // strTest -> [1, 2, 2, 1] String strTest = Arrays.toString(test); // strTest -> 1221 strTest = strTest.replaceAll(", ", "").replace("[", "").replace("]", ""); System.out.print(strTest.substring(0, 2) + " == " + strTest.substring(2, 4) + " : "); // checks 12 and 21 if(strTest.substring(0, 2).equals(strTest.substring(2, 4))) System.o
Find elsewhere
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ java-string-substring
Master Java Substring Method: Examples, Syntax, and Use Cases | DigitalOcean
February 20, 2025 - The substring() method extracts a single portion of a string, while split() divides a string into an array based on a delimiter.
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ lang โ€บ String.html
String (Java Platform SE 8 )
October 20, 2025 - A String object is returned, representing the substring of this string that begins with the character at index k and ends with the character at index m-that is, the result of this.substring(k, m + 1). This method may be used to trim whitespace (as defined above) from the beginning and end of ...
๐ŸŒ
HappyCoders.eu
happycoders.eu โ€บ java โ€บ substring
Java substring() Method
June 12, 2025 - As explained above, as of Java 7, the substring returned by String.substring() points to a separate char array.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ string-arrays-in-java
String Arrays in Java - GeeksforGeeks
October 2, 2025 - In this article, we will learn the concepts of String Arrays in Java including declaration, initialization, iteration, searching, sorting, and converting a String Array to a single string.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 25566232 โ€บ how-can-i-store-substrings-generated-from-a-string-into-a-string-array
java - How can I store substrings generated from a String into a String array? - Stack Overflow
String word = "WOMAN"; String[] res = new String[20]; String sub; int i, j, k=0; int len = word.length(); for(i = 0;i < len;i++) { for(j = 0;j <= len-i;j++,k++) { sub = word.substring(i, i+j); //for(k = 0;k < res.length;k++) res[k] = sub; } } ... String word = "WOMAN"; String sub = ""; int len = word.length(); List list = new ArrayList(); for(int i = 0; i < len; i++) { for(int j = 1; j <=len-i; j++) { sub = word.substring(i, i+j); list.add(sub); } } System.out.println(list);
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ string-to-array-in-java-how-to-convert-a-string-to-an-array-in-java
String to Array in Java โ€“ How to Convert Strings to Arrays
July 6, 2023 - The split() method in Java is a convenient way to split a string into an array of substrings based on a specified delimiter.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ substring
Java Substring - javatpoint
Substring in Java. A part of string is called the substring. The java substring() method of String class is used for getting a substring from the string.
๐ŸŒ
DataFlair
data-flair.training โ€บ blogs โ€บ java-string-substring-method
Java String substring() Method with Examples - DataFlair
December 1, 2025 - It then calculates the length of the substring, which is from beginIndex to the end of the original string. The substring is created by copying the range from the internal character array (value) into a new array using copyOfRange().
Top answer
1 of 7
9

As pointed out by Pete Kirkham, this is implementation specific. My answer is only correct for the Sun JRE, and only prior to Java 7 update 6.

You're right about a normal substring call just creating a new string referring to the same character array as the original string. That's what happens on line 5 too. The fact that the new string object reference happens to be assigned to a variable doesn't change the behaviour of the method.

Just to be clear, you say that in line 2 the new string will still point to "Monday" - the char array reference inside the string will be to the same char array as one used for "Monday". But "Monday" is a string in itself, not a char array. In other words, by the time line 2 has finished (and ignoring GC) there are two string objects, both referring to the same char array. One has a count of 6 and the other has a count of 3; both have an offset of 0.

You're wrong about line 4 using a "string pool" though - there's no pooling going on there. However, it is different to the other lines. When you call the String(String) constructor, the new string takes a copy of the character data of the original, so it's completely separate. This can be very useful if you only need a string which contains a small part of a very large original string; it allows the original large char array to be garbage collected (assuming nothing else needs it) while you hold onto the copy of the small portion. A good example of this in my own experience is reading lines from a line. By default, BufferedLineReader will read lines using an 80-character buffer, so every string returned will use a char array of at least 80 characters. If you're reading lots of very short lines (single words) the difference in terms of memory consumption just through the use of the odd-looking

line = new String(line);

can be very significant.

Does that help?

2 of 7
7

I know that line 2 will still point to "Monday" and have a new String object with the offset and count set to 0,3.

That is currently true of the Sun JRE implementation. I seem to recall that was not true of the Sun implementation in the past, and is not true of other implementations of the JVM. Do not rely on behaviour which is not specified. GNU classpath might copy the array (I can't remember off hand what ratio is uses to decide when to copy, but it does copy if the copy is a small enough fraction of the original, which turned one nice O(N) algorithm to O(N^2)).

The line 4 will create a new String "Mon" in string pool and point to it.

No, it creates a new string object in the heap, subject to the same garbage collection rules as any other object. Whether or not it shares the same underlying character array is implementation dependant. Do not rely on behaviour which is not specified.

The String(String) constructor says:

Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string.

The String(char[]) constructor says:

Allocates a new String so that it represents the sequence of characters currently contained in the character array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the newly created string.

Following good OO principles, no method of String actually requires that it is implemented using a character array, so no part of the specification of String requires operations on an character array. Those operations which take an array as input specify that the contents of the array are copied to whatever internal storage is used in the String. A string could use UTF-8 or LZ compression internally and conform to the API.

However, if your JVM doesn't make the small-ratio sub-string optimisation, then there's a chance that it does copy only the relevant portion when you use new String(String), so it's a case of trying it a seeing if it improves the memory use. Not everything which effects Java runtimes is defined by Java.

To obtain a string in the string pool which is equal to a string, use the intern() method. This will either retrieve a string from the pool if one with the value already has been interned, or create a new string and put it in the pool. Note that pooled strings have different (again implementation dependent) garbage collection behaviour.

๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ home โ€บ java โ€บ java string substring
Java String Substring
September 1, 2008 - The substring begins with the character at the specified index and extends to the end of this string or up to endIndex 1, if the second argument is given. ... The specified substring. import java.io.*; public class Test { public static void ...