# How to compare strings in Java

There are three practical options for comparing strings in Java. Java’s `equals()` method, the `==` operator and the Java method `compareTo()` are all solid choices.

## What are the options for comparing strings in Java?

While the [primitive data types](https://www.ionos.com/digitalguide/websites/web-development/java-primitives/) can easily be compared with each other in Java using the [Java operator](https://www.ionos.com/digitalguide/websites/web-development/java-operators/) `==`, it’s a bit more complicated for strings. You can use the `==` operator with strings, but it will only check if they **refer to the same object**, not whether their content is the same. In some cases that might suffice or even be what you’re looking for, but in other cases you’ll want a way to check whether strings contain the same text. Luckily there are several options in this [programming language](https://www.ionos.com/digitalguide/websites/web-development/web-programming-languages/) for getting the result you’re looking for. They include:

- Java `equals()`
- Java `compareTo()` for strings
- Java Operator `==`

## How to use Java `equals()`

The simplest and most practical option for comparing the content of two strings is the `equals()` method. It compares the characters in the two strings. If they match exactly, the method returns `true`. If they differ in any way, it returns `false`. Here’s the syntax for the method:

```java
public boolean equals(object1 object2)
```

Let’s look at a simple example. We’ll create three strings and compare the first of these strings with the other two. In practice, it will look as follows:

```java
public class Main {
	public static void main(String[] args) {
	String string1 = "This is a string";
	String string2 = "This is a string";
	String string3 = " This is a different string";
	System.out.println(string1.equals(string2));
	System.out.println(string1.equals(string3));
	}
}
```

If we use the [Java command](https://www.ionos.com/digitalguide/websites/web-development/java-commands/) `System.out.println` to print the output of this code, we get:

```java
true
false
```

`string1` and `string2` are identical, whereas `string3` is different from the other two strings. While in this example we can easily recognize this with our eyes, there are other strings that might look the same at first glance but differ with regard to some small detail. Java’s `equals()` is a great way to determine whether they are in fact the same.

## How to use Java’s `compareTo()`

The second method is a little bit more complicated, but also reliable. Java’s `compareTo()` method compares strings **lexicographically based on the Unicode value of the individual characters** in the strings. Each of the individual characters is checked against the others. If the strings match, the method will return 0. If the first string is shorter than the second, it will return a negative value. If it’s longer, it will return a positive value. Here’s the syntax for the method:

```java
public int compareTo(string1 string2)
```

Let’s now look at a simple example. We’ll once again create three strings and compare the first with the other two:

```java
public class Main {
	public static void main(String[] args) {
	String string1 = "This is a string";
	String string2 = "This is a string";
	String string3 = "This is a completely different string";
	System.out.println(string1.compareTo(string2));
	System.out.println(string1.compareTo(string3));
	}
}
```

When output the code with `System.out.println`, we get:

```java
0
16
```

Java’s compareTo() is case sensitive. If you want to compare strings without regard for lowercase and uppercase letters, use the modified method `compareToIgnoreCase()`. It works as follows:

```java
public class Main {
	public static void main(String[] args) {
	String string1 = "This is a string";
	String string2 = "This is a string";
	String string3 = "THIS IS A STRING";
	System.out.println(string1.compareToIgnoreCase(string2));
	System.out.println(string1.compareToIgnoreCase(string3));
	}
}
```

The output shows that the strings are identical:

```java
0
0
```

## How to use `==` to compare strings in Java

As mentioned above, you can use the `==` operator to compare two strings with each other. However, it will compare the references of the strings and not the actual values. Let’s see how this works with an example:

```java
public class Main {
	public static void main(String[] args) {
	String string1 = "This is a string";
	String string2 = "This is a string";
	String string3 = new String ("This is a string");
	System.out.println(string1==string2);
	System.out.println(string1==string3);
	}
}
```

Since `string1` and `string2` refer to the same instance, the output will be `true` when they are compared. But `string3` refers to another instance. This means that the output will be `false`, even though the text within the two strings is identical.

```java
true
false
```


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