Home Tutorial Java Corejava Nio Compare buffer to another object.

 
 

Compare buffer to another object.
Posted on: July 27, 2010 at 12:00 AM
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

Related Tags for Compare buffer to another object.:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.