Java StringBuffer compareTo() Method



The Java StringBuffer compareTo() method compares two StringBuffer instances in a lexicographical manner. However, the lexicographical order defined in this method is different from the usual technique. Let us look into its definition below −

Consider two StringBuffer objects of a certain length to be a sequence of char values. Suppose k is the lowest index at which the corresponding char values from each sequence differ. To order these sequences lexicographically, we compare the char values at kth index of both sequences numerically. However, if there is no such index k, the shorter sequence is considered lexicographically less than the other; but if the sequences have the same length, the sequences are considered lexicographically equal.

Note: This method synchronizes on this, the current object, and not the StringBuffer argument with which this StringBuffer is compared.

Syntax

Following is the syntax of the Java StringBuffer compareTo() method

public int compareTo(StringBuffer another)

Parameters

  • another − the StringBuffer to be compared with

Return Value

The method returns:

  • the value 0 if this StringBuffer contains the same character sequence as that of the argument StringBuffer.
  • a negative integer if this StringBuffer is lexicographically less than the StringBuffer argument.
  • or a positive integer if this StringBuffer is lexicographically greater than the StringBuffer argument.

Example

If the StringBuffer argument passed to the method has the same character sequence as the input StringBuffer, the return value is obtained as 0.

In the following example, let us look at the usage of Java StringBuffer compareTo() method.

import java.lang.*;

public class StringBuffercompareTo {
   public static void main(String[] args) {

      //create a  StringBuffer object
      StringBuffer obj1 = new StringBuffer("abcdef");

      //create a  StringBuffer object
      StringBuffer obj2 = new StringBuffer("abcdef");

      //print the stream generated
      System.out.println("Comparing '" + obj1 + "' with '" + obj2 + "': " + obj1.compareTo(obj2));
   }
}

Output

Compile and run the given program to produce the output as follows −

Comparing 'abcdef' with 'abcdef': 0

Example

If the StringBuffer argument passed to the method has the character sequence with lesser lexicographical order than the input StringBuffer, the return value is obtained as a positive integer.

import java.lang.*;

public class StringBuffercompareTo {
   public static void main(String[] args) {

      //create a  StringBuffer object
      StringBuffer obj1 = new StringBuffer("abcdef");

      //create a  StringBuffer object
      StringBuffer obj2 = new StringBuffer("123456");

      //print the stream generated
      System.out.println("Comparing '" + obj1 + "' with '" + obj2 + "': " + obj1.compareTo(obj2));
   }
}

Output

Compile and run the given program to produce the output as follows −

Comparing 'abcdef' with '123456': 48

Example

If the StringBuffer argument passed to the method has the character sequence with greater lexicographical order than the input StringBuffer, the return value is obtained as a negative integer.

import java.lang.*;

public class StringBuffercompareTo {
   public static void main(String[] args) {

      //create a  StringBuffer object
      StringBuffer obj1 = new StringBuffer("784932");

      //create a  StringBuffer object
      StringBuffer obj2 = new StringBuffer("djhahi");

      //print the stream generated
      System.out.println("Comparing '" + obj1 + "' with '" + obj2 + "': " + obj1.compareTo(obj2));
   }
}

Output

Compile and run the given program to produce the output as follows −

Comparing '784932' with ' djhahi': -45
java_lang_stringbuffer.htm
Advertisements