The Java contentEquals method is one of the Java String Method which is used to compare the string with user specified data and check whether they both represent the same characters or not, and based on the result it will return Boolean True or False. In this article we will show you, How to write contentEquals in Java Programming language with example. Before we get into the example, let us see the basic 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. Following method will accept the Character Sequence as the argument and check whether the string is equal to this Character Sequence or not
1 2 3 4 |
public boolean contentEquals(CharSequence seq); // It will return boolean True or False //In order to use in program String_Object.contentEquals(CharSequence seq) |
Following method will accept the String Buffer as the argument and check whether the string is equal to this string buffer or not
1 2 3 4 |
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
The Java string contentEquals method is used to compare whether the string and user specified data represents same char values or not. In this Java program, We are going to find the same.
JAVA CODE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// 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); } } |
OUTPUT
ANALYSIS
First we declared three String variables str1, str2, str3 and assigned corresponding values using following statement
1 2 3 |
String str1 = "Java Programming"; String str2 = "Java Program"; String str3 = "Tutorial Gateway"; |
Next, we declared the Character Sequence, StringBuffer and assigned corresponding values using following statement
1 2 |
CharSequence str4 = "Java Program"; StringBuffer str5 = new StringBuffer("Java Programming"); |
Following statements 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.
1 2 |
boolean a = str1.contentEquals(str4); boolean b = str2.contentEquals(str4); |
Following statements will call the public 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.
1 2 |
boolean c = str1.contentEquals(str5); boolean d = str2.contentEquals(str5); |
Following statement will compare 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”
1 |
boolean e = str3.contentEquals(str4); |
Next we used the character sequence directly inside the string.contentEquals method
1 |
boolean f = str3.contentEquals("Tutorial Gateway"); |
Lastly, we used the System.out.println statements will to print the output
Java contentEquals Example
In this Java program, We are going to ask the user to enter any word and based the user entered string value we will display the message.
JAVA CODE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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"); } } } |
OUTPUT
Let us enter different word
ANALYSIS
Following statement will ask the user to enter any word, and then we are going to assign the user entered value to string variable str.
1 2 |
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 be printed
- Else System.out.println(“Goodbye Tutorial Gateway”); statement will be printed
Thank You for Visiting Our Blog
Share your Feedback, or Code!!