Write a program to compare a given string to the specified string buffer


In the String_Compare_Buffer program, we are comparing two strings str1 and str2 with a StringBuffer object buffer using the contentEquals() method.

First, we create two strings str1 and str2 with values "Tutor Joes" and "Computer Education" respectively. Then we create a StringBuffer object buffer with the value of str1.

Next, we use the contentEquals() method to compare str1 and buffer, and str2 and buffer. The contentEquals() method returns true if the characters in the string are the same as the characters in the specified CharSequence, and false otherwise.

Source Code

public class String_Compare_Buffer
{
	public static void main(String[] args)
	{
		String str1 = "Tutor Joes";
		String str2 = "Computer Education";
		StringBuffer buffer = new StringBuffer(str1);
 
		System.out.println("Comparing '"+str1+"'"+" and '"+buffer+"' : "+ str1.contentEquals(buffer));
		System.out.println("Comparing '"+str2+"'"+" and '"+buffer+"' : "+ str2.contentEquals(buffer));		
	}
}

Output

Comparing 'Tutor Joes' and 'Tutor Joes' : true
Comparing 'Computer Education' and 'Tutor Joes' : false

Example Programs