How to use Java strings

Java strings are one of the complex data types in Java. They are used to represent and modify a sequence of characters or digits. There are a number of methods available to help you modify strings. In this tutorial we provide an introduction to what you need to know about strings in Java.

What are Java strings and what are they used for?

The programming language Java has many different data types. The most important ones include Java primitives like integer and char, and complex data types like strings. A string is a sequence of characters or digits that exists as an object in the java.lang class. Strings are created with the String class and then can no longer be changed. Despite this fact, Java strings are still used more often than StringBuilder, which allows you to modify strings after they’ve been created. Below we’ll explain how to create a string and what methods you have at your disposal.

Web Hosting
Fast, scalable hosting for any website
  • 99.9% uptime
  • PHP 8.3 with JIT compiler
  • SSL, DDoS protection, and backups

What is the syntax for Java strings?

If you want to create a new string in Java, you can use this simple syntax:

String name = "string";
java

You signal to the system that you want to create a Java string, give the string a name and then enter the characters of the string between the quotation marks. If you want to refer to that string later, use the name you gave to it. Here’s what the code looks like for creating a string:

public class Main {
	public static void main(String[] args) {
	String example = "Here’s your first Java string.";
	System.out.println(example);
 }
}
java

The output looks as follows:

Here’s your first Java string.
java

What are the methods used with Java strings?

Since Java strings are objects, there are a variety of methods you can use with them. Below we’ll introduce the most important ones.

length()

The length() method is used to output the total number of character values in a string. You’ll get the length of your string as output. Here’s the syntax:

stringName.length()
java

It’s also very simple to apply. The code looks as follows:

public class Main {
	public static void main(String[] args) {
	String example = "Here’s your first Java string.";
	int length = example.length();
	System.out.println("The string is " + length + " characters long.");
 }
}
java

And you’ll get the following output:

The string is 30 characters long.
java

indexOf()

You can use the indexOf() method to locate one or more substrings within your larger string. Here’s the syntax for it:

stringName.indexOf()
java

To use the method, simply enter the part of the string that you want to locate.

public class Main {
	public static void main(String[] args) {
	String example = "Here’s your first Java string.";
	System.out.println(example.indexOf("your"));
 }
}
java

As output you’ll get the position at which that text first appears:

7
java

concat()

With concat() you can connect two separate strings to form a new string. Here is its syntax:

stringName.concat(string1)
java

Here’s how the method works in practice:

public class Main {
	public static void main(String[] args) {
	String example = "word";
	String newString = example.concat("smith");
	System.out.println(newString);
 }
}
java

Here’s the output:

wordsmith
java

equals()

equals() is one of many methods you can use to compare Java strings with each other. The method returns a Boolean value (true or false). Here is its syntax:

string1.equals(string2)
java

In our example, we’ll create three strings and then compare the first one with the other two:

public class Main {
	public static void main(String[] args) {
	String string1 = "ABC";
	String string2 = "ABC";
	String string3 = "DEF";
	System.out.println(string1.equals(string2));
	System.out.println(string1.equals(string3));
 }
}
java

Since the first and second strings are identical and the third is not, you’ll get the following output:

true
false
java

toUpperCase() und toLowerCase()

The two methods toUpperCase() and toLowerCase() serve to convert the character values within a string to uppercase or lowercase. Here is the syntax for each of them:

stringName.toUpperCase()
stringName.toLowerCase()
java

Here’s how they work:

public class Main {
	public static void main(String[] args) {
	String example = "Here’s your first Java string.";
	System.out.println(example.toUpperCase());
	System.out.println(example.toLowerCase());
 }
}
java

Here’s the output:

HERE’S YOUR FIRST JAVA STRING.
here's your first java string.
java

split()

With the Java split() method you can divide a Java string into smaller substrings. Here is its syntax:

String[] split(String regex)
java

This is what it looks like in practice:

public class Main {
	public static void main(String[] args) {
		String x = "Here’s your first Java string";
		String[] output = x.split(" ");
		for (String y : output) {
			System.out.println(y);
		}
	}
}
java

The output looks like this:

Here’s
your
first
Java
string.
java
Was this article helpful?
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top