Java contentEquals Method

The Java contentEquals method is a Java String Method, which is to compare the string with user-specified data and check whether they both represent the same characters or not. Based on the result, the contentEquals method will return Boolean True or False.

In this article, we will show how to write contentEquals in Java with an example. The syntax of the string.contentEquals in Java Programming language is as shown below.

Java contentEquals syntax

The Java Programming Language provides two different contentEquals methods. The following Java contentEquals 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 Java contentEquals 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 Java program, We are going to use the Java string contentEquals method to compare whether the string and user-specified data represent the same char values or not.

// Java String.contentEquals example
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

First, we declared the Character Sequence, StringBuffer, and assigned corresponding values using the following statement.

CharSequence str4 = "Java Program";
StringBuffer str5 = new StringBuffer("Java Programming");

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

boolean a = str1.contentEquals(str4);
boolean b = str2.contentEquals(str4);

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 above screenshot, you can observe that c is returning True, and d is returning False.

boolean c = str1.contentEquals(str5);
boolean d = str2.contentEquals(str5);

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

boolean e = str3.contentEquals(str4);

Next, we used the character sequence directly inside the contentEquals String Method method

boolean f = str3.contentEquals("Tutorial Gateway");

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

Java contentEquals Example

In this Java program, We are going to 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 different word

Please Enter any word: 
program
Goodbye Tutorial Gateway

Within this contentEquals example, the following statement will ask the user to enter any word. Then we are going to assign the user entered value to string variable str.

System.out.println("Please Enter any word: ");
String str = sc.nextLine();

Next, we used the Java If Else Statement to compare the user entered string with character sequence “java” and check whether they are equal or not.

  • If the statement inside the If is True, System.out.println(“Hey!! Welcome to Java Programming Language”); statement will print.
  • Else System.out.println(“Goodbye Tutorial Gateway”); statement will print.