ShortBuffer in java, Compare a short buffer with another short buffer.


 

ShortBuffer in java, Compare a short buffer with another short buffer.

In this tutorial, you will see how to compare a short buffer with another short buffer.

In this tutorial, you will see how to compare a short buffer with another short buffer.

Compare a short buffer with another short buffer.

 In this tutorial, we will see how to compare a short buffer with another short buffer.

ShortBuffer API:

The java.nio.ShortBuffer class extends java.nio.Buffer class. It provides the following methods:

Return type Method Description
static ShortBuffer allocate( int capacity) The allocate(..) method allocate a short buffer of given capacity.
int compareTo(ShortBuffer buffer) The compareTo(...) method returns 0, -1 or 1. It means, this buffer is equal, less and greater than to another buffer.
abstract  ShortBuffer put(short s) The put(..) method write a given short value at the current position.

code

import java.nio.*;
import java.nio.ShortBuffer;

public class CompareToShortBuffer {
  public static void main(String[] argsthrows Exception {
    ShortBuffer shortBuf = ShortBuffer.allocate(1024);
    shortBuf.put((short4);
    shortBuf.put((short5);
    shortBuf.flip();
    ShortBuffer shortBuf1 = ShortBuffer.allocate(1024);
    shortBuf1.put((short98);
    if ((shortBuf.compareTo(shortBuf1)) == 0) {
      System.out.println("Both short buffer are equal.");
    else if ((shortBuf.compareTo(shortBuf1)) == 1) {
      System.out
.println("This short buffer is greater than to other short buffer.");
    else {
      System.out
   .println("This short buffer is less than to other short buffer.");
    }
    ShortBuffer shortBuf2 = ShortBuffer.allocate(1024);
    shortBuf2.put((short76);
    if ((shortBuf2.compareTo(shortBuf1)) == 0) {
      System.out.println("Both short buffer are equal.");
    else if ((shortBuf2.compareTo(shortBuf1)) == 1) {
      System.out
.println("This short buffer is greater than to other short buffer.");
    else {
      System.out
   .println("This short buffer is less than to other short buffer.");
    }
    if ((shortBuf2.compareTo(shortBuf)) == 0) {
      System.out.println("Both short buffer are equal.");
    else if ((shortBuf2.compareTo(shortBuf)) == 1) {
      System.out
.println("This short buffer is greater than to other short buffer.");
    else {
      System.out
   .println("This short buffer is less than to other short buffer.");
    }
  }
}

Output

C:\>java CompareToShortBuffer
This short buffer is greater than to other short buffer.
Both short buffer are equal.
This short buffer is less than to other short buffer.

Download this code

Ads