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;
Where imya is the name of the variable.
Examples:
| Command | Description |
|---|---|
|
A string variable name is created |
|
A string variable message is created |
|
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;
2. Assigning values to String variables
To put a value into a variable of type String, use the command:
imya = "value";
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 |
|---|---|
|
The variable name contains the text Anya |
|
The variable city contains the text New York |
|
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";
String name = "Anya", city = "New York", message = "Hello!";
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"
Examples:
| Command | Note |
|---|---|
|
name contains the string AnyaAnya |
|
city contains the string New YorkAnya |
|
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:
" "
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”:
""
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 |
|---|---|---|
|
String length | |
|
To upper case | |
|
To lower case | |
|
Trim whitespace at both ends | |
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.
GO TO FULL VERSION