How to Compare Two StringBuffer Objects in Java


The example is to show comparing two StringBuffer objects through contentsEquals() method in Java.

Source Code

package com.beginner.examples;

public class CompareStringBufferStringExamples {

	public static void main(String[] args) {
	
		String str = "I love java";
		
		StringBuffer sb = new StringBuffer();
		
		sb.append("I love java");
		/*To compare the contents of the two objects, 
		 *we can use the contentEquals() method in the String object*/
		if(str.contentEquals(sb)) {
			System.out.println("The content in both objects is the same");
		}else {
			System.out.println("The content in the two objects is different");
		}
		
		
	}

}

Output:

The content in both objects is the same
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments