So far, we have worked with numbers, characters, and other types of data. But what if we want to store text such as a person's name, an address, or a message?
For this, Java provides String.
A String is a sequence of characters. For example, "Hello", "John", and "Welcome to Java!" are all Strings.
Creating a String
We can create a String and store it in a variable like this:
String name = "John";
Here, String is the data type, name is the variable, and "John" is the String value.
We can also store a complete sentence:
String message = "Welcome to Java!";
Strings are written inside double quotation marks.
String city = "London";
Remember that a char stores a single character using single quotes:
char grade = 'A';
While a String can contain multiple characters using double quotes:
String grade = "A";
Even though "A" contains only one character, it is still a String because it uses double quotes.
Printing a String
We can print a String using System.out.println():
String name = "John";
System.out.println(name);
Output:
John
We can also combine Strings with other values using the + operator:
String name = "John";
int age = 25;
System.out.println("Name: " + name);
System.out.println("Age: " + age);
Output:
Name: John
Age: 25
Finding the Length of a String
Java provides the length() method to find how many characters a String contains.
String name = "John";
System.out.println(name.length());
Output:
4
John contains four characters, so length() returns 4.
Spaces are also counted:
String message = "Hello Java";
System.out.println(message.length());
The space between Hello and Java is also counted as a character.
Accessing Characters
We can get an individual character from a String using the charAt() method.
For example:
String name = "John";
System.out.println(name.charAt(0));
Output:
J
Just like arrays, String indexes start from 0.
So:
J o h n
0 1 2 3
Therefore:
name.charAt(2);
returns:
h
Converting to Uppercase and Lowercase
Java provides methods for changing the case of a String.
toUpperCase() converts the String to uppercase:
String name = "John";
System.out.println(name.toUpperCase());
Output:
JOHN
Similarly, toLowerCase() converts it to lowercase:
System.out.println(name.toLowerCase());
Output:
john
These methods don't change the original String. They return a new String containing the converted text.
Comparing Strings
When working with Strings, we often need to check whether two Strings contain the same text.
For this, we commonly use the equals() method:
String name1 = "John";
String name2 = "John";
System.out.println(name1.equals(name2));
Output:
true
If the text is different:
String name1 = "John";
String name2 = "Jason";
System.out.println(name1.equals(name2));
the result is:
false
For now, remember that equals() is used to compare the contents of Strings.
Joining Strings
We can join two or more Strings using the + operator.
String firstName = "John";
String lastName = "Smith";
String fullName = firstName + " " + lastName;
System.out.println(fullName);
Output:
John Smith
The + operator joins the Strings together. We added " " between the names to create a space.
Removing Extra Spaces
Sometimes a String may contain unnecessary spaces at the beginning or end.
We can use the trim() method to remove those spaces:
String name = " John ";
System.out.println(name.trim());
Output:
John
This is useful when processing text entered by users.
A Simple Example
Let's put some String operations together:
class Main {
public static void main(String[] args) {
String name = "John";
System.out.println("Name: " + name);
System.out.println("Length: " + name.length());
System.out.println("First character: " + name.charAt(0));
System.out.println("Uppercase: " + name.toUpperCase());
}
}
Output:
Name: John
Length: 4
First character: J
Uppercase: JOHN
Strings are used everywhere in programming because applications constantly work with text. Usernames, messages, email addresses, search queries, product names, and many other pieces of information are represented using Strings.
As you continue learning Java, you'll use Strings very frequently, so getting comfortable with the basic String operations is important.