Home Tutorial Java Corejava Nio Compare a float buffer with another float buffer.

 
 

Compare a float buffer with another float buffer.
Posted on: July 30, 2010 at 12:00 AM
In this tutorial you will see how to compare a float buffer with another float buffer.

Compare a float buffer with another float buffer.

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

FloatBuffer API:

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

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

code

import java.nio.*;
import java.nio.FloatBuffer;

public class FloatCompareDemo {
public static void main(String[] args){
    FloatBuffer floatBuf = FloatBuffer.allocate(256);
    floatBuf.put(4.2f);
    floatBuf.put(5.1f);
    floatBuf.put(6.4f);
    FloatBuffer floatBuf1 = FloatBuffer.allocate(256);
    floatBuf1.put(98.54f);
    floatBuf1.put(5.3f);
    int i=floatBuf.compareTo(floatBuf1);
    System.out.println(i);    
    FloatBuffer floatBuf2 = FloatBuffer.allocate(256);
    floatBuf2.put(76.45f);
    floatBuf2.put(98.32f);
    int i1=floatBuf2.compareTo(floatBuf1);
    System.out.println(i1);
    int i2=floatBuf2.compareTo(floatBuf);
    System.out.println(i2);
    }
  }

Output

C:\>java FloatCompareDemo
-1
0
1

Download this code

Related Tags for Compare a float buffer with another float buffer.:


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.