String contentEquals(StringBuffer sb) method java

Let’s learn String contentEquals(StringBuffer sb) method java.

String contentEquals(StringBuffer sb) method java

String contentEquals(StringBuffer sb) method compares this string to the specified StringBuffer.

The result is true if and only if this String represents the same sequence of characters as the specified StringBuffer.

Syntax

public boolean contentEquals(StringBuffer sb)

Returns

true if this String represents the same sequence of characters as the specified StringBuffer, false otherwise.

Example

class StringContentEqualsMethodDemo 
{
   public static void main(String[] args) 
   {
      String s1 = "Hello World";
      String s2 = "Hello World";
      StringBuffer sb = new StringBuffer(s1);
      System.out.println("Returns : " + s2.contentEquals(sb));
      s2 = s1.toLowerCase();
      System.out.println("Returns : " + s2.contentEquals(sb));
   }
}

Output:

Returns : true
Returns : false


Also read – if else in java