Java Strings

Beginner
8 min read
Updated: Nov 2026
5 examples
java.lang.String

Definition and Usage

In Java, a String is text: an object of java.lang.String. It stores a sequence of characters—names, messages, file paths, JSON, and almost every piece of text a program prints or reads.

You create strings with double-quoted literals ("Hello"), join them with +, and print them with System.out.println. Strings are immutable: once created, their characters do not change in place.

This page teaches what a Java String is. The String Methods index lists every method guide (charAt(), equals(), contains(), and more).

Use equals() for text.

== compares object references. Two strings with the same characters can still be different objects—always prefer equals() for content.

Immutable returns.

concat(), toUpperCase(), and similar methods return a new String. Assign the result if you need the updated text.

Java Syntax

Common ways to create and inspect strings:

Java
String literal = "Hello, Java!";   // double-quoted literal
String built   = first + " " + last; // + concatenation
int len        = text.length();      // number of char units
char ch        = text.charAt(0);     // character at index
boolean same   = text.equals("Hi");  // compare content

Core Ideas

Ideas every Java string tutorial builds on:

IdeaExampleRemember
Literal"Hello"Most common way to create a String
Concatenationa + " " + bCreates a new String
Lengthtext.length()Method (with parentheses)
Charactertext.charAt(0)Zero-based index
Content equalitya.equals(b)Not == for text
Immutabilitytext.toUpperCase()Returns a new String

Java Examples

Five short programs that define how strings work in a Java tutorial.

Example 1 — Create and print

Assign text and display it with System.out.println.

Java
public class StringsIntro {
    public static void main(String[] args) {
        String language = "Java";
        int version = 21;

        System.out.println("Language: " + language);
        System.out.println("Version: " + version);
        System.out.println("Ready to learn strings!");
    }
}

How It Works

  1. String language stores a literal.
  2. + joins a string and a number (the number becomes text).
  3. println prints each line with a newline.

Example 2 — Escapes

Quotes, backslashes, and newlines inside a string.

Java
public class StringLiterals {
    public static void main(String[] args) {
        String quote = "She said, \"Hello!\"";
        String path  = "C:\\Users\\Dev\\project";
        String lines = "Line 1\nLine 2";

        System.out.println(quote);
        System.out.println(path);
        System.out.println(lines);
    }
}

How It Works

  1. \" inserts a double quote.
  2. \\ inserts a backslash.
  3. \n starts a new line.

Example 3 — Join with + and concat()

Build a message from smaller pieces.

Java
public class StringConcat {
    public static void main(String[] args) {
        String first = "Hello";
        String last  = "Java";

        String greeting = first + ", " + last + "!";
        System.out.println(greeting);

        String status = "Status: ";
        status = status.concat("Active");
        System.out.println(status);
    }
}
concat() Tutorial

How It Works

  1. + is the everyday join operator.
  2. concat() also returns a new String—assign it.
  3. StringBuilder is better for many joins in a loop.

Example 4 — length(), charAt(), equals()

Inspect characters and compare content safely.

Java
public class StringMembers {
    public static void main(String[] args) {
        String word = "Hello";
        String copy = new String("Hello");

        System.out.println("length: " + word.length());
        System.out.println("first: " + word.charAt(0));
        System.out.println("equals: " + word.equals(copy));
        System.out.println("== : " + (word == copy));
    }
}
equals() Tutorial

How It Works

  1. length() counts UTF-16 code units.
  2. charAt(0) is the first character.
  3. equals is true (same text).
  4. == is false (new String is a different object).

Next: Java String Methods Index

After you know what a String is, open the method index for full tutorials on every essential java.lang.String API in this series.

Start withWhy
charAt()Read a character by index
equals()Safe content comparison
contains()Substring search
format()Printf-style messages
length()Count the characters in a string
substring()Cut out part of a string by index
indexOf()Find where a character or word starts
split()Break text into an array of pieces
replace()Swap characters or words (returns a new String)
trim()Remove spaces from both ends

Full list: Java String Methods index (/java/string/method).

Frequently Asked Questions

A String is an object of java.lang.String that stores a sequence of characters—names, messages, paths, and more. You usually create one with a double-quoted literal like "Hello".
No. Strings are immutable. Methods like concat() and toUpperCase() return a new String. For many edits in a loop, use StringBuilder.
Use equals() to compare text content. == checks whether two variables point to the same object in memory—identical text can still be different objects.
Use +: String full = first + " " + last;. You can also call concat(). Prefer StringBuilder when joining many pieces in a loop.
Practice the examples on this page, then open the String Methods index for charAt(), equals(), contains(), format(), and every other method guide in this Java tutorial series.

Browse every String method

Open the methods index for categorized guides with examples and FAQs.

String Methods Index →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful