Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

What is java.nio.FloatBuffer class in Java?

Muhammad Ashir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The java.nio class represents New IO, an alternative to the regular IO API in Java. It allows you to do non-blocking IO using channels and buffers. A channel is like a regular stream, but unlike a stream, a single channel can be used to read and write data, whereas a stream is usually only one-way. Channels can be written to asynchronously. Channels usually write to and read from buffers. A buffer is a block of memory where data can be written and be read later on. It is a finite sequence of elements of any particular primitive type.

Note: Even though java.nio supports non-blocking IO, it is NOT an asynchronous class because some operations are actually blocking, e.g., file IO.

Buffers have three essential properties:

  1. Capacity: Capacity refers to the total size of the buffer; it always remains constant.
  2. Limit: The limit determines how many elements you can read or write from the buffer.
  3. Position: The position determines the index from where reading or writing to a buffer should be initiated.

The FloatBuffer class

The java.nio.FloatBuffer class extends the java.nio.Buffer superclass. It defines the following essential operations that can be performed on FloatBuffer:

  1. Absolute and relative put and get methods for reading and writing data from the buffer as a single Float element.

  2. Absolute and relative bulk put and get methods for reading and writing data from the buffer or a Float array as an array.

  3. Methods for slicing, compacting, and duplicating the buffer.

Creating a FloatBuffer

A FloatBuffer can be created using either the allocate() method which allocates a fixed amount of space for the buffer or by wrapping an existing Float array into a buffer using the wrap() method or by creating a view buffer.

import java.nio.*;
import java.util.*;
class HelloWorld {
public static void main( String args[] ) {
int CAPACITY = 10;
float [] floatArray = {1.0F, 2.5F};
FloatBuffer buff1 = FloatBuffer.allocate(CAPACITY);
FloatBuffer buff2 = FloatBuffer.wrap(floatArray);
buff1.put(1, 99.0F);
System.out.println(Arrays.toString(buff1.array()));
System.out.println(Arrays.toString(buff2.array()));
}
}

The code above shows an example of creating a FloatBuffer object. We create two buffers, buff1 and buff2. buff1 is created using the allocate() method, and buff2 is made by wrapping an already existing float array called floatArray into a buffer using the wrap() method.

The .array() method returns the float array that backs up the buffer. Then, we use the .toString() method to print it out to the standard output.

Class methods

Method

Description

allocate(int capacity)

Allocates a new float buffer. Returns static FloatBuffer.

array()


Returns the float array that backs this buffer  (optional operation).

arrayOffset()

Returns the offset as an int within this buffer's backing array of the first element of the buffer  (optional operation).

asReadOnlyBuffer()

Creates a new, read-only float buffer that shares this buffer's content.

compact()

Compacts this buffer  (optional operation).

compareTo(FloatBuffer that)

Compares this buffer to another.

duplicate()

Creates a new float buffer that shares this buffer's content.

equals(Object ob)

Tells whether or not this buffer is equal to another object. Returns boolean.

get()

Relative get method. Returns float.

get(float[] dst)

Relative bulk get method.

get(float[] dst, int offset, int length)

Relative bulk get method.

put(float f)

Relative put method  (optional operation).

put(float[] src)

Relative bulk put method  (optional operation).

put(int index, float f)

Absolute put method  (optional operation).

A complete list of the class methods can be found here.

RELATED TAGS

java

CONTRIBUTOR

Muhammad Ashir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring