What is the FloatBuffer allocate() method in Java?

FloatBuffer is a built-in class available in the java.nio.FloatBuffer package.

FloatBuffer implements a floating-point buffer and allows the user to perform various categories of operations upon buffers of datatype float.

allocate() is a method defined in the FloatBuffer class. The following is the function prototype:

public static FloatBuffer allocate(int capacity)

Parameters and return value

The allocate() method takes the following input parameter:

  • capacity: An integer value that represents the size of the FloatBuffer.

The method returns the new FloatBuffer. If the capacity provided is negative, then an IllegalArgumentException is thrown.

Functionality

The allocate() method allocates a new FloatBuffer. The size of the buffer is set to the capacity passed as the input argument.

Once the method is invoked, each element of the buffer is initialized to zero. The offset is also set to zero by default.

The FloatBuffer.allocate() method

Code

import java.nio.*;
import java.util.*;
public class Edpresso {
public static void main(String[] args)
{
// Declaring capacity
int capacity = 10;
// Creating new FloatBuffer
FloatBuffer floatbuffer = FloatBuffer.allocate(capacity);
// Putting the value in FloatBuffer
floatbuffer.put(7.86F);
floatbuffer.put(4, 3.14F); // placing on 4th index
// Printing the FloatBuffer
System.out.println(Arrays.toString(floatbuffer.array()));
}
}

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved