The offset/count fields are somewhat orthogonal to the pooling/intern() issues. Offset and count come when you have something like:

String substring = myString.substring(5);

One way to implement this method would be something like:

  • allocate a new char[] with myString.length() - 5 elements
  • copy all of the elements from index index 5 to myString.length() from myString to the new char[]
  • substring is constructed with this new char[]
    • substring.charAt(i) goes directly to chars[i]
    • substring.length() goes directly to chars.length

As you san see, this approach is O(N) -- where N is the new string's length -- and requires two allocations: the new String, and the new char[]. So instead, substring works by resusing the original char[] but with an offset:

  • substring.offset = myString.offset + newOffset
  • substring.count = myString.count - newOffset
  • use myString.chars as the chars array for substring
    • substring.charAt(i) goes to chars[i+substring.offset]
    • substring.length() goes to substring.count

Note that we didn't need to create a new char[], and more importantly, we didn't need to copy the chars from the old char[] to the new one (since there is no new one). So this operation is just O(1) and requires only one allocation, that of the new String.

Answer from yshavit on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
April 21, 2026 - The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.string
String Class (Java.Lang) | Microsoft Learn
The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.
🌐
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 - The implementation of string conversion is typically through the method toString, defined by Object and inherited by all classes in Java.
Top answer
1 of 4
3

The offset/count fields are somewhat orthogonal to the pooling/intern() issues. Offset and count come when you have something like:

String substring = myString.substring(5);

One way to implement this method would be something like:

  • allocate a new char[] with myString.length() - 5 elements
  • copy all of the elements from index index 5 to myString.length() from myString to the new char[]
  • substring is constructed with this new char[]
    • substring.charAt(i) goes directly to chars[i]
    • substring.length() goes directly to chars.length

As you san see, this approach is O(N) -- where N is the new string's length -- and requires two allocations: the new String, and the new char[]. So instead, substring works by resusing the original char[] but with an offset:

  • substring.offset = myString.offset + newOffset
  • substring.count = myString.count - newOffset
  • use myString.chars as the chars array for substring
    • substring.charAt(i) goes to chars[i+substring.offset]
    • substring.length() goes to substring.count

Note that we didn't need to create a new char[], and more importantly, we didn't need to copy the chars from the old char[] to the new one (since there is no new one). So this operation is just O(1) and requires only one allocation, that of the new String.

2 of 4
2

Java always uses references to any object. There's no way to make it not use references. As for string pooling, that is achieved by the compiler for string literals and at runtime by calling String.intern. It is natural that most of the implementation of String is oblivious to whether it is dealing with an instance referred to by the constant pool or not.

🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › lang › String.html
String (Java SE 17 & JDK 17)
April 21, 2026 - The implementation of string conversion is typically through the method toString, defined by Object and inherited by all classes in Java.
🌐
GeeksforGeeks
geeksforgeeks.org › java › strings-in-java
Java Strings - GeeksforGeeks
Using the new keyword creates a new object in heap memory, even if the same string already exists in the pool. ... The CharSequence interface represents a sequence of characters in Java.
Published   1 week ago
🌐
GitHub
github.com › openjdk › jdk › blob › master › src › java.base › share › classes › java › lang › String.java
jdk/src/java.base/share/classes/java/lang/String.java at master · openjdk/jdk
* string literals in Java programs, such as {@code "abc"}, are · * implemented as instances of this class. * <p> * Strings are immutable; their values cannot be changed after they · * are created. Because String objects are immutable they can be shared.
Author   openjdk
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › String.html
String (Java Platform SE 7 )
The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the ...
Find elsewhere
🌐
W3Schools
w3schools.com › java › java_strings.asp
Java Strings
assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof int interface long module native new package private protected public return requires short static super switch synchronized this throw throws transient try var void volatile while Java String Methods
🌐
Oracle
docs.oracle.com › en › java › javase › 22 › docs › api › java.base › java › lang › String.html
String (Java SE 22 & JDK 22)
July 16, 2024 - The implementation of string conversion is typically through the method toString, defined by Object and inherited by all classes in Java.
🌐
Software Testing Help
softwaretestinghelp.com › home › java › java string methods tutorial with examples
Java String Methods Tutorial With Examples
April 1, 2025 - This method is used to determine whether a substring is a part of the main String or not. The return type is Boolean. For E.g. In the below program, we will check whether “testing” is a part of “Softwaretestinghelp” or not and we will also check whether “blog” is a part of “Softwaretestinghelp”. package codes; import java.lang.String; public class StringMethods { public static void main(String[] args) { String str = "Softwaretestinghelp"; String str1 = "testing"; String str2 = "blog"; System.out.println("testing is a part of Softwaretestinghelp: " + str.contains(str1)); System.out.println("blog is a part of Softwaretestinghelp: " + str.contains(str2)); } }
🌐
Oracle
docs.oracle.com › en › java › javase › 23 › docs › api › java.base › java › lang › String.html
String (Java SE 23 & JDK 23)
October 17, 2024 - The implementation of string conversion is typically through the method toString, defined by Object and inherited by all classes in Java.
🌐
Simplilearn
simplilearn.com › home › resources › software development › java tutorial for beginners › what are java strings and how to implement them?
What are Java Strings and How To Implement Them
July 23, 2024 - What are Java strings and how do you create them? Learn all about Java strings, the methods based on Java strings, sub strings, and more with example programs.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Classpath
developer.classpath.org › doc › java › lang › String-source.html
Source for java.lang.String (GNU Classpath 0.95 Documentation)
82: * 83: * @author Paul N. Fisher ... public final class String 92: implements Serializable, Comparable<String>, CharSequence 93: { 94: // WARNING: String is a CORE class in the bootstrap cycle. See the comments 95: // in vm/reference/java/lang/Runtime for implications of this ...
🌐
Oracle
docs.oracle.com › en › java › javase › 25 › docs › api › java.base › java › lang › String.html
String (Java SE 25 & JDK 25)
January 20, 2026 - The implementation of string conversion is typically through the method toString, defined by Object and inherited by all classes in Java.
🌐
ScholarHat
scholarhat.com › home
What is String in Java - Java String Methods & Type (Examples)
In Java, the String class implements the CharSequence interface, which represents a readable sequence of characters. One of the notable implementations of this interface is CharBuffer, which allows efficient character manipulation using buffers.
Published   August 30, 2025
🌐
Oracle
docs.oracle.com › javase › 10 › docs › api › java › lang › String.html
String (Java SE 10 & JDK 10 )
The implementation of string conversion is typically through the method toString, defined by Object and inherited by all classes in Java.
🌐
The Knowledge Academy
theknowledgeacademy.com › blog › java-string
Java String: What are they and how to implement them?
January 1, 2009 - Here, the CharSequence Interface represents the sequence of characters, while Serialization in Java is reflected through the implementation of the Serializable interface, enabling the conversion of objects to a byte stream. Apart from that, StringBuffer and StringBuilder Class in Java also implement the CharSequence interface.
🌐
Edureka
edureka.co › blog › java-string
Java Strings - How to Declare String in Java With Examples
February 13, 2025 - For Example: String s=“Welcome”; By new keyword : Java String is created by using a keyword “new”. For example: String s=new String(“Welcome”); It creates two objects (in String pool and in heap) and one reference variable where ...
🌐
DevOps.dev
blog.devops.dev › java-internals-string-c3721ed91e6d
Java Internals — String - DevOps.dev
April 8, 2024 - The String class in Java implements immutability by declaring its instance variables final and providing no methods for directly modifying the value of a String object.