---
title: Compare strings
url: https://docs.vultr.com/java/examples/compare-strings
publish_date: 2024-11-12T15:20:00.087202Z
last_updated: 2024-11-12T15:20:00.087202Z
header_image: https://assets.vultrdevrel.com/docs-main-doc-assets-1/collection-items/769/8dcd1457-ba9c-44f4-ba0b-0078e2a14169.jpg
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

# Java Program to Compare Strings

## Introduction

When it comes to programming in Java, comparing strings is a fundamental operation that you may encounter in various scenarios such as sorting, searching, and user authentication. String comparison is unique because it's not just about checking for equality in character sequence, but also involves memory address locations, reference checks, and case sensitivity issues.

In this article, you will learn how to compare strings in Java using different methods. You'll see how each approach works through relevant examples, ensuring you understand when to use each method based on the scenario.

## Using the equals() Method

### Check Exact Match
1. Create two string variables for comparison.
1. Use the `equals()` method to compare the two strings.

    ```java
    String str1 = "Hello";
    String str2 = "Hello";
    boolean isEqual = str1.equals(str2);
    System.out.println("String equality: " + isEqual);
    ```

    This code snippet compares `str1` and `str2` for equality in their characters and case. The output will be `true` because both strings are identical in content and case.

## Using the equalsIgnoreCase() Method

### Compare Ignoring Case
1. Initialize strings in different cases.
1. Apply `equalsIgnoreCase()` to compare irrespective of case.

    ```java
    String str1 = "hello";
    String str2 = "HELLO";
    boolean isEqualIgnoreCase = str1.equalsIgnoreCase(str2);
    System.out.println("Case insensitive equality: " + isEqualIgnoreCase);
    ```

    Here, even though `str1` and `str2` have different cases, `equalsIgnoreCase()` considers them equal. The method will return `true`.

## Using the == Operator

### Check String Interning
1. Understand that `==` checks if two references point to the same object.
1. Compare two string references using `==`.

    ```java
    String str1 = "Hello";
    String str2 = "Hello";
    boolean isSameReference = (str1 == str2);
    System.out.println("Reference equality: " + isSameReference);
    ```

    In most cases, literal strings like in this example are interned by the JVM, meaning `str1` and `str2` point to the same memory location. Thus, the output here is `true`.

1. Show scenario where `==` gives `false`.

    ```java
    String str1 = new String("Hello");
    String str2 = new String("Hello");
    boolean isSameReference = (str1 == str2);
    System.out.println("Reference equality with new: " + isSameReference);
    ```

    Here, `str1` and `str2` are created with `new`, so they refer to different objects. Despite having the same content, the output will be `false`.

## Using the compareTo() Method

### Sorting Strings Lexicographically
1. Use the `compareTo()` method to determine the lexicographical order.
1. Compare two strings.

    ```java
    String str1 = "Apple";
    String str2 = "Banana";
    int result = str1.compareTo(str2);
    System.out.println("Lexicographical comparison: " + result);
    ```

    This method returns a negative number because "Apple" comes before "Banana" alphabetically. The value indicates the difference between the first non-matching characters.

## Conclusion

Java provides several methods for comparing strings, each suitable for different circumstances. Use `equals()` for a direct character-by-character comparison, `equalsIgnoreCase()` for a case-insensitive check, `==` for reference comparison, and `compareTo()` for determining lexicographical ordering. Understanding the nuances of each method allows you to choose the most appropriate string comparison technique for your Java applications, enhancing both functionality and performance.