# How to use Java strings

Java strings are one of the complex [data types in Java](https://www.ionos.com/en-ie/digitalguide/websites/web-development/java-data-types/). 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](https://www.ionos.com/en-ie/digitalguide/websites/web-development/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](https://www.ionos.com/en-ie/digitalguide/websites/web-development/java-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.

## What is the syntax for Java strings?

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

```java
String name = "string";
```

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:

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

The output looks as follows:

```java
Here’s your first Java string.
```

## 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:

```java
stringName.length()
```

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

```java
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.");
 }
}
```

And you’ll get the following output:

```java
The string is 30 characters long.
```

### `indexOf()`

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

```java
stringName.indexOf()
```

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

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

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

```java
7
```

### `concat()`

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

```java
stringName.concat(string1)
```

Here’s how the method works in practice:

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

Here’s the output:

```java
wordsmith
```

### `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:

```java
string1.equals(string2)
```

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

```java
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));
 }
}
```

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

```java
true
false
```

### `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:

```java
stringName.toUpperCase()
stringName.toLowerCase()
```

Here’s how they work:

```java
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());
 }
}
```

Here’s the output:

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

### `split()`

With the [Java split()](https://www.ionos.com/en-ie/digitalguide/websites/web-development/string-splitting-in-java/) method you can **divide a Java string into smaller substrings**. Here is its syntax:

```java
String[] split(String regex)
```

This is what it looks like in practice:

```java
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);
		}
	}
}
```

The output looks like this:

```java
Here’s
your
first
Java
string.
```


This is a markdown version of: [https://www.ionos.com/en-ie/digitalguide/websites/web-development/java-strings/](https://www.ionos.com/en-ie/digitalguide/websites/web-development/java-strings/) for AI/LLM consumption.