CodeGym /Courses /JAVA 25 SELF /Strings and text: the String type, working with strings

Strings and text: the String type, working with strings

JAVA 25 SELF
Level 2 , Lesson 2
Available

1. Taking a closer look at the String type

The String type is one of the most commonly used types in Java — perhaps the most used of all. It’s a fantastic type, because you can store text in such variables. And unlike the int and double types, String objects have lots of different functions (methods), and with these functions you can do all sorts of interesting things.

Moreover, all objects in Java (literally all of them) can be converted to the String type. Or, to be more precise, every object in Java returns its textual (string) representation. We’ll come back to this type more than once — it’s very useful and interesting — but today we’ll cover the basics.

Creating variables of type String

The String type is designed to store strings (text). To declare a variable in code that can hold text, use the command:

String imya;
Creating a variable of type String

Where imya is the name of the variable.

Examples:

Command Description
String name;
A string variable name is created
String message;
A string variable message is created
String text;
A string variable text is created

Just like with the int and double types, you can use a shorthand to declare several variables of type String:

String imya1, imya2, imya3;
Creating variables of type String

2. Assigning values to String variables

To put a value into a variable of type String, use the command:

imya = "value";
Assigning a value to a variable of type String

And here’s the first difference from the types you’ve already studied. All values of type String are strings of text and must be enclosed in double quotes.

Examples:

Command Note
String name = "Anya";
The variable name contains the text Anya
String city = "New York";
The variable city contains the text New York
String message = "Hello!";
The variable message contains the text Hello!

3. Initializing String variables

As with the int and double types, variables of type String can be initialized right when they are created. In fact, you can do this with any type in Java. So we won’t talk about it anymore.

String imya1 = "value1", imya2 = "value2", imya3 = "value3";
Shorthand form of declaring and initializing variables
String name = "Anya", city = "New York", message = "Hello!";
An example of declaring and initializing variables

You can only use variables after assignment

The Java compiler will complain if you declare a variable without assigning any value and then try to use it.

This code will not work:

String name;
System.out.println(name); // Variable name is not initialized. The program will not compile.
int a;
a++; // Variable a is not initialized. The program will not compile.
double x;
double y = x; // Variable x is not initialized. The program will not compile.

4. Concatenation — joining strings

There’s a very handy and simple thing you can do with strings in Java — you can join them together. This operation is called concatenation. Or, if it helps you remember — con-cat-enation 😆. It’s often called “joining strings” or even just “string addition.”

To join two strings, use the + sign. It’s very simple:

"string1" + "string2"
Concatenation of two strings

Examples:

Command Note
String name = "Anya" + "Anya";
name contains the string AnyaAnya
String city = "New York" + "Anya";
city contains the string New YorkAnya
String message = "Hello! " + "Anya";
message contains the string Hello! Anya

And of course, you can concatenate many strings at once, as well as concatenate strings and variables.

String name = "Anya";                // name contains the string Anya
String city = "New York";           // city contains the string New York
String message = "Hello!" + city + name + city; // message contains the string Hello!New YorkAnyaNew York

5. Empty strings

In the last example you can see that the text in the message variable is hard to read, because it lacks spaces. To add one space or several, simply write them in the code and wrap them in double quotes. It’s simpler than it looks:

" "
A string containing a single space

By the way, if you leave nothing between the quotes (write two double quotes in a row), you get what’s called an “empty string”:

""
Empty string

On the one hand, it sort of exists, but on the other, when you print it to the screen, nothing is shown. And when it’s concatenated with other strings, nothing changes. It’s like the analogue of zero, but for strings.

6. Escaping special characters in strings

You already know that strings are enclosed in double quotes. But what if you need to insert quotes into a string? If we put quotes inside the string, won’t the compiler treat them as the end of the string?

Correct — it will. That’s why quotes inside text are replaced with the pair of characters \". And the code will look like this:

String quote = "He said: \"Hello!\"";
System.out.println(quote); // He said: "Hello!"

In fact, it’s a bit trickier. The character \ is considered a special (escape) character inside strings. With it you can specify various “non-printing characters” like newline, tab, etc. And to specify the \ character itself, you need to write it twice.

Here are the 4 most common combinations:

Notation Meaning
\n Newline
\t Tab (indent)
\\ A literal \
\" Double quote inside a string

Example:

String multiline = "Line 1\nLine 2";
System.out.println(multiline);

Output:

Line 1
Line 2

7. String methods

The String type has its own functions (also called methods). There are a lot of them, and they make developers’ lives easier. Today you’ll get acquainted with some of the simplest ones. Example:

Method Description Example result
str.length()
String length
"abc".length() → 3
str.toUpperCase()
To upper case
"abc".toUpperCase() → "ABC"
str.toLowerCase()
To lower case
"ABC".toLowerCase() → "abc"
str.trim()
Trim whitespace at both ends
"  x y  ".trim() → "x y"

Examples of working with string methods

All these methods are called like this: variable.method(...).

Get the length of a string:

String name = "Andrey";
int length = name.length();
System.out.println(length); // 6, because there are 6 letters

Convert a string to upper or lower case:

String original = "Hello";
System.out.println(original.toUpperCase()); // HELLO
System.out.println(original.toLowerCase()); // hello

Trim leading and trailing spaces (very useful for user input):

String messy = "   hello   ";
System.out.println(messy.trim()); // "hello"

Each such method returns a new string; the original string does not change.

1
Task
JAVA 25 SELF, level 2, lesson 2
Locked
Message from the Future 👽
Message from the Future 👽
1
Task
JAVA 25 SELF, level 2, lesson 2
Locked
Creating a Full Name 👤
Creating a Full Name 👤
1
Task
JAVA 25 SELF, level 2, lesson 2
Locked
Hero's Quote 🗣️
Hero's Quote 🗣️
1
Task
JAVA 25 SELF, level 2, lesson 2
Locked
Processing form data 📝
Processing form data 📝
Comments (1)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Michael Otieno Level 3, Kenya
22 January 2026
Well understood