Java string

What is a Java string?

A Java string is a sequence of characters that exists as an object of the class java.lang. Java strings are created and manipulated through the string class. Once created, a string is immutable -- its value cannot be changed.

A string is sequence of characters. A class is a user-defined template for creating an object. A string class is a user-defined template for creating and manipulating string objects, which are sequences of characters.

Methods of class string enable the following capabilities:

  • examining individual characters in the string;
  • comparing strings;
  • searching strings; and
  • copying strings with characters converted from uppercase to lowercase or vice versa.

Creating a Java string

Below is one simple syntax for creating a Java string.

String greeting = "Hello world!";

In this example, "Hello world!" is a string literal, which is a series of characters encased in quotation marks. The compiler will create an object with that value, and the string value is written into the source code of a computer program.

Whenever a new string variable or object is created, it is stored in computer memory.

Memory is split into two high-level blocks, the stack and the heap. Java stores object values in heap memory; references to the value are stored in the stack.

Another way to create strings is to use the new keyword, as in the following example.

String s1 = new String("Hello world!");

That code will create an s1 string object and a reference variable.

As noted, Java strings are immutable. Once created, a string using string class cannot be changed. Any attempted changes will create another string instance.

Users can perform basic operations, such as comparing strings. The .equals() method is used to compare strings, as in the following example.

String str = "one";
String st = "one";
System.out.println (str.equals(st));

The above code will compare the two values of each string to see if they are the same. In this case they are, so the Java program returns the Boolean value "true." 

Types of strings in Java

Java distinguishes between primitive strings and object strings:

  • Primitive strings. These are string literals or string calls from a nonconstructor context. A constructor is a special method used to initialize objects. Primitives are not objects and have no methods or properties. They are represented at the lowest level of language implementation.
  • Object strings. These are strings created using the new operator. Object strings create two objects, whereas primitives create just one. Object strings create the string literal and the variable to refer to it.

The two string types are stored in memory differently.

When a string literal is created, the Java virtual machine (JVM) checks the string pool to see if it already exists. The string constant pool is a memory area where strings are stored. If the value exists, the string primitive will occupy the existing value. If the value does not exist, the JVM creates a new string and adds it to the pool.

The string constant pool lives inside the memory heap. When an object string is created, the part between the double quotes goes into the string constant pool. The variable assigned to the string is stored in the stack and matched to the string inside the pool.

The memory heap provides global access to all data stored in the heap for the entire life of the Java application. All object values are stored in the heap, including string literals, which are stored in the string constant pool inside the heap. Primitive variables and references to strings are stored in the stack and are matched to values in the heap. 

Stack and heap image
References to objects reside in the stack. The objects themselves reside in the heap. String values reside in the string pool inside the heap.

Java string object example

In Java, a string is an object that represents a number of character values. Each letter in the string is a separate character value that makes up the Java string object. Characters in Java are represented by the char class. Users can write an array of char values that will mean the same thing as a string.

The following is an example of how character values make up the char array ch.

char[] ch = {'t','e','c','h','t','a','r','g','e','t'};
String s2 = new String (ch);

This means the same thing as the example below.

String s2 = "techtarget";

In the example, each letter of the string is represented by a character. Although strings created using the string class are immutable, strings created using combinations of characters can be changed. There are several utility string methods that can be used to alter strings, including split(), toLowerCase(), toUpperCase(), length() and valueOf(). These string methods can do the following:

  • split strings at a specified point, such as white spaces;
  • make the string lowercase or uppercase;
  • give the string length; and
  • provide the value of a given character in a string.

The following shows the use of valueOf() for determining the value of a letter in the above char array ch.

public class StringValueOfExample5 {
public static void main(String[] args) {
     char[] ch = {'t','e','c','h','t','a','r','g','e','t'};
     String s2 = new String (ch);
     String v = String.valueOf(ch[1]);

The example above prints the value of the first position in the character array ch. The exact print statement is: e.

Utility methods like valueOf() and toLowerCase() are only used with strings that are immutable. They perform operations on strings, but they do not change the string itself. To create mutable strings that can be changed, the StringBuilder() and StringBuffer() classes are used.

Below are examples of string mutability using these classes.

String concatenation, which is the process of linking strings together, can be done using the append() method to print the sentence "Ben eats vegetables."

class Example {
  public static void main(String args[]) {
    StringBuilder x = new StringBuilder("Ben ");
    x.append("eats vegetables");

Reverse a string using the reverse() method to print "selbategev stae neB."

class Example{
   public static void main (String args[]) {
       StringBuilder x = new StringBuilder ("Ben eats vegetables");

Delete a specified portion of a string using the delete() method. This prints just the word "vegetables."

class Example {
  public static void main (String args[]) {
    StringBuilder x = new StringBuilder ("Ben eats vegetables");
    sb.delete(1, 9);

The classes and StringBuffer() are similar, however StringBuilder() is newer and not thread-safe, meaning it can't be used by multiple threads at once.

Java strings are a core component of the Java programming language and of software development in general. Learn about the Jenkins development platform and some possible alternatives for Java developers.

This was last updated in August 2022

Continue Reading About Java string

Dig Deeper on Core Java APIs and programming techniques

App Architecture
Software Quality
Cloud Computing