Menu

Java Strings: Create, Concatenate, and Use String Methods

How Java strings work - creating them, joining with +, why they are immutable, comparing with equals, and the everyday String methods like length, substring, and replace.

This page includes runnable editors - edit, run, and see output instantly.

What a String Is

A String holds a sequence of characters - text like a name, a sentence, or a line read from a file. You saw String listed among the reference types on the previous page; here you'll actually work with one. You write a string literal with double quotes (single quotes are reserved for a single char):

name.length() returns the number of characters - 3 for "Ada". It's a method call, so it needs the parentheses.

Joining Strings

The + operator glues strings together. If either side of + is a string, the other side is converted to text automatically, so you can mix in numbers and booleans without any extra work:

One gotcha: because + works left to right, "" + 1 + 2 produces "12" (string + int, then string + int), but 1 + 2 + "" produces "3" (the two ints add first, then become text). When in doubt, wrap arithmetic in parentheses.

Strings Are Immutable

This is the single most important thing to understand about Java strings: a String never changes after it's created. Every method that "modifies" a string actually returns a brand-new string and leaves the original untouched. So if you forget to capture the return value, nothing happens:

The first call computes "HELLO" and immediately discards it. Only the second line, which assigns back into s, has any visible effect. This trips up nearly everyone once.

Comparing Strings

Use .equals() to compare the contents of two strings. Reaching for == is the classic beginner mistake: == asks "are these the same object in memory?", not "do they hold the same text?":

a == b is false here because new String(...) forces a separate object. Always compare string values with .equals() (or .equalsIgnoreCase() to ignore capitalization). A handy trick to avoid a crash when one side might be null is to call .equals on the literal: "yes".equals(input) never throws even if input is null.

Everyday String Methods

Because strings are immutable, each of these returns a new string (or a value) rather than changing the original. These are the ones you'll reach for constantly:

Notice the chaining: s.trim().toUpperCase() works because trim() returns a string, which you then call toUpperCase() on. substring is another workhorse - it takes a start index (inclusive) and optional end index (exclusive):

Indexes are zero-based, so substring(0, 4) grabs characters 0 through 3. Asking for an index past the end throws a StringIndexOutOfBoundsException - one of the runtime exceptions you can guard against. Note too that split(",") above hands you an array, where length is a field with no parentheses, unlike the .length() method on a string.

Building Strings in a Loop

Concatenating with + is fine for a few pieces, but doing it inside a large loop is slow - each + creates a new string and copies everything. When you assemble text in a loop, use StringBuilder, which mutates a buffer in place and produces the final string once at the end:

append returns the same builder, so the calls chain. Call .toString() to get a regular String out at the end. For one-off joins, stick with +; for accumulating many pieces, StringBuilder is the right tool.

Next: String Format

Concatenation gets messy once you need aligned columns, fixed decimal places, or padded numbers. Java's String.format and printf use a template with % placeholders to handle all of that cleanly - that's the next page.

Frequently Asked Questions

How do you compare two strings in Java?

Use .equals() for content: a.equals(b) returns true when the characters match. Do not use == to compare string values - == compares object references, so it can return false for two strings that hold the same text. Use .equalsIgnoreCase() when case should not matter.

Why are Java strings immutable?

Once created, a String object can never change. Methods like replace or toUpperCase return a new string instead of modifying the original. Immutability makes strings safe to share and cache, but it means you must assign the result: s = s.toUpperCase();. If you call a method and ignore its return value, nothing happens.

How do you get the length of a string in Java?

Call the .length() method: "hello".length() returns 5. Note it is a method with parentheses on a String, unlike arrays where length is a field with no parentheses (arr.length).

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED