Java String endsWith() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In Java, endsWith() checks whether a string ends with a given suffix. It returns true or false.

You pass the suffix. It is required. The check is case-sensitive by default.

Syntax

Call endsWith on the string. Start with this line.

java
boolean ok = file.endsWith(".java");
CallWhat it does
file.endsWith(".java")Returns true when file ends with .java.

This only checks the end. For a match anywhere, use contains().

Library declarations
java
public boolean endsWith(String suffix);

Parameters

ParameterDescription
suffixRequired. The ending text to look for. Do not pass null.

Return Value

Return ValueDescription
trueThe string ends with the suffix.
falseThe string does not end with the suffix.

Java Examples

Example 1 — Basic Suffix Check

Test whether a greeting ends with a known word.

java
public class EndsWithBasic {
    public static void main(String[] args) {
        String text = "Hello, Java!";

        System.out.println(text.endsWith("Java!"));
        System.out.println(text.endsWith("Hello"));
    }
}

How It Works

  1. Store the text. text ends with "Java!".
  2. First check. endsWith("Java!") matches the last five characters → true.
  3. Second check. "Hello" is at the start, not the end → false.
  4. Boolean result. Use it directly in println or an if.

Example 2 — Check a File Extension

A common real use: accept only .java source files.

java
public class EndsWithFile {
    public static void main(String[] args) {
        String file = "Main.java";

        if (file.endsWith(".java")) {
            System.out.println("Java source file");
        } else {
            System.out.println("Not a .java file");
        }
    }
}

How It Works

  1. Read the name. file is "Main.java".
  2. Test the suffix. endsWith(".java") looks only at the end.
  3. Branch on the result. true prints the success message.
  4. Keep the dot. Include . in the suffix so "xjava" does not match by accident.

Example 3 — Case Sensitivity

endsWith is case-sensitive. Normalize both sides when extensions may vary in case.

java
public class EndsWithCase {
    public static void main(String[] args) {
        String file = "Report.PDF";

        System.out.println(file.endsWith(".pdf"));
        System.out.println(file.toLowerCase().endsWith(".pdf"));
    }
}

How It Works

  1. Exact match fails. "PDF" ≠ "pdf" → first line is false.
  2. Lowercase the string. toLowerCase() turns it into "report.pdf".
  3. Check again. Now endsWith(".pdf") succeeds → true.
  4. Keep the suffix lower too. Compare against a lowercase suffix after normalizing.

Frequently Asked Questions

endsWith() checks whether a string ends with a given suffix and returns true or false.
suffix is required.
Yes by default.
An empty suffix always returns true.
endsWith() only checks the end. contains() looks anywhere.

Next: equals()

Compare two strings by content (not with ==).

equals() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful