Summary: in this tutorial, you will learn about Java String and how to manipulate strings effectively.
Introduction to Java String
In Java, a string is a sequence of characters. Unlike primitive data types like int
, char
, and float
, Java strings are objects. Java uses the String
class to create and manipulate strings.
Declare a string
To declare a string variable, you use the String
class. For example:
String message;
Code language: Java (java)
After having the string variable, you can assign a string literal to it. To construct a string literal, you place the text inside double quotes like this:
message = "Hello";
Code language: Java (java)
Like other variables, you can declare a String
variable and initialize its values at the same time:
String message = "Hello";
Code language: Java (java)
Getting the length of a string
To get the total of characters of a string, you use the length()
method as follows:
str.length()
Code language: Java (java)
where str
is a String.
For example, the following displays the length of the string message
with the value "Hello"
:
public class App {
public static void main(String[] args) {
String message = "Hello";
System.out.println(message.length());
}
}
Code language: Java (java)
Output:
5
Code language: Java (java)
Concatenate two strings
To concatenate two strings into a single string, you use the +
operator. For example:
public class App {
public static void main(String[] args) {
String greeting = "Hello " + "John";
System.out.println(greeting);
}
}
Code language: Java (java)
In this example, we concatenate the strings "Hello "
and "John"
into a single string, assign it to the message
variable, and output it to the screen.
Output:
Hello John
Code language: Java (java)
Java also allows you to use +=
operator to append a string into another string. For example:
public class App {
public static void main(String[] args) {
String message = "Hello ";
message += "John";
System.out.println(message);
}
}
Code language: Java (java)
Output:
Hello John
Code language: Java (java)
Accessing individual characters
To access individual characters in a string, you can use the charAt()
method.
Java String uses zero-based indexing. It means that the first character has an index of 0, the second character has an index of 1, and so on.
The following example shows how to get the first and last character of the String "Hello"
:
public class App {
public static void main(String[] args) {
String message = "Hello";
char firstCharacter = message.charAt(0);
char lastCharacter = message.charAt(message.length()-1);
System.out.println(firstCharacter);
System.out.println(lastCharacter);
}
}
Code language: Java (java)
Output:
H
o
Code language: Java (java)
In this example, we get the first character by passing the index 0 and the last character by passing the index length - 1
to the charAt()
method.
Comparing two strings
To compare two strings, you don’t use the ==
operator. Instead, you use the equals()
method:
s1.equals(s2);
Code language: Java (java)
The equals()
method returns true if s1
is equal to s2
, or false otherwise.
public class App {
public static void main(String[] args) {
String s1 = "Hello";
String s2 = "Hello";
boolean isEqual = s1.equals(s2);
System.out.println(isEqual); // true
}
}
Code language: Java (java)
Output:
true
Code language: Java (java)
Escape sequences
An escape sequence represents a character that is difficult to include directly in a string. For example, newline, tab, backslash, etc.
An escape sequence starts with a backslash (\
) followed by a character or a sequence of characters.
Here are the common escape sequences in Java:
Escape Sequence | Description |
---|---|
\t | Tab |
\b | Backspace |
\n | Newline |
\r | Carriage Return |
\f | Form feed |
\' | Single quote |
\" | double quote |
\\ | Backslash |
When the Java compiler encounters escape sequences in a string, it interprets them accordingly. For example, if you want to place quotes in a string, you must use the escape sequence \"
:
public class App {
public static void main(String[] args) {
String message = "\"Java is awesome\".They said.";
System.out.println(message);
}
}
Code language: Java (java)
Output:
"Java is awesome".They said.
Code language: JavaScript (javascript)
Java Strings are immutable
In Java, strings are immutable. It means that once you create a string, you cannot change its content. For example:
String message = "Hi ";
message += "There.";
System.out.println(message); // Hi There.
Code language: Java (java)
Output:
Hi There.
Code language: Java (java)
The concatenation seems to change the string message.
But Java doesn’t modify the original string. Instead, it creates a new string with the value "Hi There."
and assigns the string to the message variable.
Summary
- Use the Java
String
class to declare a string variable. - Use double quote (
"
) to construct string literals. - Java strings are immutable.
- Use the operator
+
to concatenate two strings into a single string. - Use
length()
method to get the length of a string. - Use
equals()
method to compare two strings. - Use
charAt()
method to access individual characters in a string.