String endsWith() method is used to check whether a String ends with the specified suffix or not.

Signature of endsWith() Method:

boolean endsWith(String suffix)

Suffix is an affix which occurs at the end of a word or a phrase.

Let’s now take an example to understand String endsWith() method.

Example 1: String endsWith() method

package com.masterjavatutorial;
public class TestExample {
	public static void main(String[] args) {
       
		String str = "HelloWorld";
		boolean flag = str.endsWith("World");
		System.out.println("HelloWorld ends wirh World ? "+flag);
		
		boolean flag1 = str.endsWith("Worl");
		System.out.println("HelloWorld ends wirh Worl ? "+flag1);

	}
}

Output of the above method is as below:

HelloWorld ends wirh World ? true
HelloWorld ends wirh Worl ? false
What if it is checked whether a String ends with null or not?

In this if would throw NullPointerException. Below is the demo program for this.

package com.masterjavatutorial;
public class TestExample {
	public static void main(String[] args) {
		String str = "HelloWorld";
		boolean flag = str.endsWith(null);
		System.out.println("HelloWorld ends wirh null ? "+flag);
	}
}

Output of the above program is as below:

package com.masterjavatutorial;
public class TestExample {
	public static void main(String[] args) {
		String str = "HelloWorld";
		boolean flag = str.endsWith(null);
		System.out.println("HelloWorld ends wirh null ? "+flag);
	}
}
What if it is checked whether a String ends with blank String or not?

In this case, it would return true. All not null string if checked whether it ends with blank string or not, would always return true.
Below is the demo program for this.

package com.masterjavatutorial;
public class TestExample {
	public static void main(String[] args) {
		String str = "HelloWorld";
		boolean flag = str.endsWith("");
		System.out.println("HelloWorld ends wirh blank string ? "+flag);
	}
}

Output of the above program is as below:

HelloWorld ends wirh null ? true