Skip to main content

String in Java

Learn String in Java (Java Basics, Java) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

1. Introduction A String is an immutable object of java.lang.String that holds a sequence of characters Every method that seems to change it returns a new String. Literals created with double quotes go into the String pool (in the heap ), so the same literal is stored once, while new always creates a fresh object . Since Java 9 strings use compact storage ( LATIN1 or UTF16 ) and the hash code is cached StringBuilder and StringBuffer are the mutable alternatives.

More lessons in Java Basics