FloatBuffer

FloatBuffer is a class in the java.nio package that represents a buffer of 32-bit floating-point numbers (floats). It is part of Java’s NIO (New Input/Output) API designed for efficient memory-based data manipulation—especially useful in graphics, gaming, scientific computing, and real-time processing.FloatBuffer is an abstract class. You create instances using these static allocate() and wrap() factory methods.

Commonly Used Methods

Simple Program: Storing Temperature Readings

import java.nio.FloatBuffer;

public class FloatBufferSimple {
    public static void main(String[] args) {
        // Allocate buffer for 5 float values
        FloatBuffer buffer = FloatBuffer.allocate(5);

        // Put some temperature readings
        buffer.put(36.5f);
        buffer.put(37.2f);
        buffer.put(36.8f);
        buffer.put(37.0f);
        buffer.put(36.9f);

        // Flip to prepare for reading
        buffer.flip();

        System.out.println("Temperature readings:");
        while (buffer.hasRemaining()) {
            System.out.println(buffer.get() + "°C");
        }
    }
}

Problem Statement

Mahesh has developed a health monitoring system that collects CPU temperature readings every second as float values.

He challenged LotusJavaPrince to:

  • Accept 10 float temperature values representing CPU readings.
  • Store them in a FloatBuffer.
  • Find and display the temperatures above 37.0°C with their index.
  • Use buffer operations only (no direct array indexing).

Can LotusJavaPrince use NIO techniques to solve this?

import java.nio.FloatBuffer;

public class CpuTemperatureMonitor {
    public static void main(String[] args) {
        // Simulated CPU temperature readings in °C
        float[] temperatures = {
            36.5f, 37.4f, 36.8f, 37.8f, 36.7f,
            38.0f, 37.1f, 36.6f, 37.3f, 36.9f
        };

        // Wrap the float array into a FloatBuffer
        FloatBuffer tempBuffer = FloatBuffer.wrap(temperatures);

        System.out.println("Temperatures above 37.0°C:");

        // Iterate using buffer methods only
        for (int i = 0; i < tempBuffer.limit(); i++) {
            float reading = tempBuffer.get(i);
            if (reading > 37.0f) {
                System.out.println("Index " + i + " → " + reading + "°C");
            }
        }
    }
}

Output

Temperatures above 37.0°C:
Index 1 → 37.4°C
Index 3 → 37.8°C
Index 5 → 38.0°C
Index 6 → 37.1°C
Index 8 → 37.3°CCode language: CSS (css)

The FloatBuffer class in Java NIO offers an efficient way to handle sequences of 32-bit floating-point numbers. As a subclass of the abstract Buffer class, it provides all the standard buffer functionalities such as managing position, limit, capacity, and mark—specifically optimized for float data.

FloatBuffer is particularly useful in applications requiring high-performance numerical computations, such as graphics processing, scientific simulations, and real-time data analysis. It supports both heap buffers and direct buffers, enabling faster native I/O operations and memory management flexibility.

With features like relative and absolute get() and put() methods, bulk data operations, and the ability to slice, duplicate, and compact buffers, FloatBuffer enables fine-grained control over floating-point data manipulation.

Scroll to Top