Java contentEquals Method

The Java contentEquals method is a String Method that compares the string with user-specified data and checks whether they both represent the same characters or not. The Java contentEquals method will return Boolean True or False based on the result. This article will show how to write contentEquals with an example.

Java contentEquals syntax

The Java Programming Language provides two different contentEquals methods. The following method will accept the Character Sequence as the argument and check whether the string is equal to this Character Sequence or not.

public boolean contentEquals(CharSequence seq); // It will return boolean True or False 

//In order to use in program
String_Object.contentEquals(CharSequence seq)

The following method will accept the String Buffer as the argument and check whether the string is equal to this string buffer or not.

public boolean contentEquals(StringBuffer Sb); // It will return boolean True or False 

//In order to use in program
String_Object.contentEquals(StringBuffer Sb)

Java contentEquals Example

In this program, We will use the string contentEquals method to compare whether the string and user-specified data represent the same char values or not.

For a and b, Java will call the public boolean contentEquals (CharSequence Seq) method to compare both the str1 and str2 with the Character Sequence str4. From the below screenshot, you can observe that a is returning False, and b is returning True.

The following statements will call the boolean contentEquals (StringBuffer Sb) method to compare both the str1 and str2 with the string buffer str4. From the below screenshot, you can observe that c is returning True, and d is returning False.

The following Java string contentEquals statement compares the string str3 with character sequence str4 for e. As we all know, this will return false because “Tutorial Gateway” is not at all equal to “Java Program”.

For boolean f, we used the character sequence directly inside the contentEquals String Method method.

Lastly, we used the Java System.out.println statements will print the output.

package StringFunctions;

public class ContentEqualsMethod {
	public static void main(String[] args) {
		String str1 = "Java Programming";
		String str2 = "Java Program";
		String str3 = "Tutorial Gateway";
		
		CharSequence str4 = "Java Program";
		StringBuffer str5 = new StringBuffer("Java Programming"); 

		boolean a = str1.contentEquals(str4);
		boolean b = str2.contentEquals(str4);
		boolean c = str1.contentEquals(str5);
		boolean d = str2.contentEquals(str5);
		
		boolean e = str3.contentEquals(str4);
		boolean f = str3.contentEquals("Tutorial Gateway");
		
		System.out.println("Does the content in str1 Equals to str4? = " + a);
		System.out.println("Does the content in str2 Equals to str4? = " + b);
		System.out.println("Does the content in str1 Equals to str5? = " + c);
		System.out.println("Does the content in str2 Equals to str5? = " + d);
		System.out.println("Does the content in str3 Equals to str4? = " + e);
		System.out.println("Does the content in str3 Equals to given string? = " + f);
	}
}
Java contentEquals Method 1

Java contentEquals Example

This Java program will ask the user to enter any word. Based on the user entered string value, the contentEquals method will display the message.

package StringFunctions;

import java.util.Scanner;

public class ContentEqualsMethodex {
	private static Scanner sc;
	public static void main(String[] args) {
		sc = new Scanner(System.in);		
		System.out.println("Please Enter any word: ");
		String str = sc.nextLine();
		
		if (str.contentEquals("java")) {
			System.out.println("Hey!! Welcome to Java Programming Language");
		}
		else {
			System.out.println("Goodbye Tutorial Gateway");
		}
	}
}
Please Enter any word: 
java
Hey!! Welcome to Java Programming Language

Let us enter a different word.

Please Enter any word: 
program
Goodbye Tutorial Gateway