Strings
A string is text
Java uses the String type for text.
A string can be written directly in code between double quotes and stored in a variable just like
a number or a boolean value.
String language = "Java";
String greeting = "Привет!";
System.out.println(language);
System.out.println(greeting);
String is not a primitive type, but at the basic level it is very simple to work with:
we create a string, pass it to methods, join it with other strings, and call ready-made operations on it.
String concatenation
Strings can be joined with the + operator. If a number,
a boolean value, or another type appears next to a string, Java converts it to text and continues concatenating.
String name = "Анна";
int lessons = 8;
String message = "Ученик: " + name + ", уроков: " + lessons;
System.out.println(message);
It is important to remember that an expression is evaluated from left to right. Until a string is encountered, numbers take part in ordinary arithmetic operations.
System.out.println(2 + 3 + " Java"); // 5 Java
System.out.println("Java " + 2 + 3); // Java 23
"Сумма: " + (2 + 3) rather than rely on concatenation order.
Length and characters
A string has the length() method, which returns its length.
An individual character can be obtained with charAt(index).
As with arrays, indexing starts at zero.
String word = "Java";
System.out.println(word.length()); // 4
System.out.println(word.charAt(0)); // J
System.out.println(word.charAt(3)); // a
The last valid index is always one less than the string length.
For example, for a string with length 4, the available indexes run from
0 to 3.
Useful string operations
Instead of processing every character manually, String already provides ready-made methods.
To begin with, it is enough to know several common operations:
substring(from, to)— take part of a string;contains(text)— check whether a fragment is present;startsWith(prefix)andendsWith(suffix)— check the beginning or end;replace(oldText, newText)— replace fragments;isEmpty()— check whether the length is zero;isBlank()— check whether the string is empty or contains only whitespace characters.
String path = "java/basics/strings";
System.out.println(path.substring(5, 11)); // basics
System.out.println(path.contains("strings")); // true
System.out.println(path.startsWith("java")); // true
System.out.println(path.replace("/", " -> "));
With substring(from, to), the starting index is included
and the ending index is excluded. The same boundary principle has already appeared when working with index ranges in arrays.
Strings are immutable
One of the main properties of String is that a created string does not change its contents.
Methods such as replace() or substring()
return a new string, while the original remains unchanged.
String text = "java";
String upper = text.toUpperCase();
System.out.println(text); // java
System.out.println(upper); // JAVA
That is why calling a method by itself does not “rewrite” the variable. If you need the result later, you must store it or use it immediately.
String status = "draft";
status.replace("draft", "ready");
System.out.println(status); // draft
status = status.replace("draft", "ready");
System.out.println(status); // ready
How to compare strings
Use the equals() method to compare string contents.
For strings, the == operator answers a different question: whether the variables
refer to the very same object. That is why you should not rely on it for ordinary text comparison.
String first = "Java";
String second = new String("Java");
System.out.println(first.equals(second)); // true
System.out.println(first == second); // false
equals().
Even if == returns true in some example,
that does not make it the correct way to compare string contents.
Special characters and multiline text
In an ordinary string literal, some characters are written with escape sequences.
For example, \n creates a line break,
\" creates a double quote,
and \\ creates a backslash.
String message = "Первая строка\nВторая строка";
String quote = "Он сказал: \"Java\"";
System.out.println(message);
System.out.println(quote);
If there is a lot of text and it spans several lines, a text block — a literal between triple quotes — is more convenient.
It lets you preserve multiline text without a chain of \n sequences and a large amount of escaping.
String json = """
{
"language": "Java",
"level": "basic"
}
""";
System.out.println(json);
The result is still the same String data type: only the way the literal is written changes.
For everyday code, it is enough to confidently create strings, concatenate them, read individual characters,
use ready-made methods, and compare contents with equals().