Share on Google+Share on Google+

Compare buffer to another object.

Advertisement
In this tutorial you will see how to compare buffer to another object.

Compare buffer to another object.

In this tutorial you will see how to compare buffer to another object. The compareTo method of CharBuffer class allow to compare buffer with another. This method returns 0, 1 or -1 which means it is equal, less and greater than the comparing buffer.  

Code:

import java.nio.CharBuffer;

public class CharBufferDemo {
  public static void main(String[] args) {
    char[] chr = new char[] { 'a''b''c''d''e' };
    CharBuffer charbuffer1 = CharBuffer.allocate(10);
    charbuffer1.put(chr);

    char[] chr1 = new char[] { 'a''b''c''d''e' };
    CharBuffer charbuffer2 = CharBuffer.allocate(10);
    charbuffer2.put(chr1);
    System.out.println(charbuffer2.compareTo(charbuffer1));

    char[] chr2 = new char[] { 'a''b''c''d''e''f' };
    CharBuffer charbuffer3 = CharBuffer.allocate(10);
    charbuffer3.put(chr2);
    System.out.println(charbuffer3.compareTo(charbuffer1));

    char[] chr3 = new char[] { 'a''b''c''d' };
    CharBuffer charbuffer4 = CharBuffer.allocate(10);
    charbuffer4.put(chr3);
    System.out.println(charbuffer4.compareTo(charbuffer1));
  }
}

Output:

 0
 -1
  1

Download this code

Advertisements

Posted on: July 27, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.