🌐
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.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
October 20, 2025 - The class String includes methods ... for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by the Character class. The Java language provides ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › substring-in-java
Java String substring() Method - GeeksforGeeks
April 11, 2025 - In Java, the substring() method of the String class returns a substring from the given string.
🌐
Career Karma
careerkarma.com › blog › java › java substring: a how-to guide
Java Substring: A How-To Guide | Career Karma
December 1, 2023 - The Java substring() method returns a portion of a particular string. This method accepts the start and end index values of the characters you would like to retrieve from a string.
🌐
The Renegade Coder
therenegadecoder.com › code › be-careful-with-strings-substring-method-in-java
Be Careful with String’s Substring Method in Java – The Renegade Coder
May 26, 2020 - Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex. Java API, 2019
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-string-substring-method-example
Java String substring() Method with examples
Note: The method is called like this: substring(start + 1, end), here start index is +1 because, we want to get the substring after the delimiter position, however the end index is not +1 because end index is not inclusive in substring method. public class JavaExample{ public static void main(String[] args){ //given string String str = "Welcome to [BeginnersBook.com]"; //we would like to get the substring between '[' and ']' int start = str.indexOf("["); int end = str.indexOf("]"); String outStr = str.substring(start + 1, end); System.out.println(outStr); } }
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.string.substring
String.Substring Method (Java.Lang) | Microsoft Learn
Returns a string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1.
🌐
Simplilearn
simplilearn.com › home › resources › software development › all you should know about substring in java
All You Should Know About Substring in Java | Simplilearn
September 11, 2025 - A substring is a part of another string. It's a commonly used method in java. Explore this tutorial to get an in-depth idea about substring in java. Start now!
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
Find elsewhere
🌐
OpenJDK
openjdk.org › jeps › 8303099
JEP draft: Null-Restricted and Nullable Types (Preview)
February 23, 2023 - In a Java program, a variable of type String may hold either a reference to a String object or the special value null. In some cases, the author intends that the variable will always hold a reference to a String object; in other cases, the author expects null as a meaningful value.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › data › manipstrings.html
Manipulating Characters in a String (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
As shown in the following figure, our extension method uses lastIndexOf to locate the last occurrence of the period (.) in the file name. Then substring uses the return value of lastIndexOf to extract the file name extension — that is, the substring from the period to the end of the string.
🌐
FavTutor
favtutor.com › blogs › java-substrings
Substring in Java (with examples)
November 5, 2023 - In Java, the substring is a powerful method that allows developers to extract subsets or parts of a string. By using the substring() method, you can create smaller strings from a bigger one without modifying the original string.
Top answer
1 of 2
9

Strings in Java are immutable. Basically this means that, once you create a string object, you won't be able to modify/change the content of a string. As a result, if you perform any manipulation on a string object which "appears to" change the content of the string, Java creates a new string object, and performs the manipulation on the newly created one.

Based on this, your code above appears to create five string objects - two are created by the declaration, two are created by calls to substring, and the last one is created after you concatenate the two pieces.

Immutability however leads to another interesting consequence. JVM internally maintains something like a string pool for creating string literals. For saving up memory, JVM will try to use string objects from this pool. Whenever you create a new string literal, JVM will loop into the pool to see if any existing strings can be used. If there is, JVM will simply use it and return it.

So, technically, before Java 7, JVM will create only one string object for your whole code. Even your substring calls won't create new string objects in the pool, it will use the existing "Hello World" one, but in this case it will only use characters from position 0 to 3 for your first call to substring, for example. Starting from Java 7, substring will not share the characters, but will create a new one. So, total object count will be 4 - the last one will be created with the concatenation of the two substrings.

Edit To answer your question in the comment, take a look at Java Language Specification -

In the Java programming language, unlike C, an array of char is not a String, and neither a String nor an array of char is terminated by '\u0000' (the NUL character).

A String object is immutable, that is, its contents never change, while an array of char has mutable elements.

The method toCharArray in class String returns an array of characters containing the same character sequence as a String. The class StringBuffer implements useful methods on mutable arrays of characters.

So, no, char arrays are not immutable in Java, they are mutable.

2 of 2
0

Literal a is created newly and kept in the pool.Literal b refer the a, it will not create new one instead.

The line 3 will create 3 new String since substring creates a new string and concatenate creates new Strings every time.

String substring(int beginIndex,int endIndex)

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

🌐
IronPDF
ironpdf.com › ironpdf for java › ironpdf for java blog › java help › java substring method
Java Substring Method (How It Works For Developers)
November 18, 2025 - The Java string substring method is part of the String class in Java. It's designed to create a new string from a portion of an existing string
🌐
Baeldung
baeldung.com › home › java › java string › get substring from string in java
Get Substring from String in Java | Baeldung
July 21, 2024 - For more details on the Java regular expressions check out this tutorial. We can use the split method from the String class to extract a substring. Say we want to extract the first sentence from the example String.
🌐
GeeksforGeeks
geeksforgeeks.org › java › stringbuilder-substring-method-in-java-with-examples
StringBuilder substring() method in Java with examples - GeeksforGeeks
December 19, 2025 - Java Collection · Last Updated : 19 Dec, 2025 · The substring() method of the StringBuilder class is used to extract a portion of characters from an existing character sequence and return it as a new String.
🌐
CodingBat
codingbat.com › doc › java-string-substring.html
Java Substring v2
There is a more complex version of substring() that takes both start and end index numbers: substring(int start, int end) returns a string of the chars beginning at the start index number and running up to but not including the end index.
🌐
Reddit
reddit.com › r/learnprogramming › how to correctly use the substring method? (java)
r/learnprogramming on Reddit: How to correctly use the substring method? (Java)
February 2, 2013 -

Hey guys, I've been working on this Java homework for my class and this whole section about string manipulation is confusing me.

The homework asks to add statements that use the substring method to get the first half and second half of phrase.

I'm assuming that means using the substring method, but I always get errors trying to compile what I put in. So here's the code. I'm not exactly sure how to use the substring method in the way they ask, and my book doesn't really go over it well. Should I be creating another variable or just use what I have?

🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › java string substring
Java String Substring
September 1, 2008 - Learn how to use the substring() method in Java to extract parts of a string effectively.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › lang › String.html
String (Java SE 11 & JDK 11 )
January 20, 2026 - A substring of this String object is compared to a substring of the argument other. The result is true if these substrings represent identical character sequences. The substring of this String object to be compared begins at index toffset and has length len. The substring of other to be compared ...