Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

What are the get() methods of the FloatBuffer class in Java?

Talha Ashar

The get() methods of the FloatBuffer class in Java return either the value at the current buffer position or the value at a specific position in a FloatBuffer object, based on the parameters passed to the function.

The process is illustrated below:

To use the get() methods, you will need to import the FloatBuffer class into your program, as shown below:

import java.nio.FloatBuffer

Variations and parameters

There are 22 variations of the get() method:

  1. The first variation of the get() method has no parameters and returns the value of the current buffer position. The current buffer position is also incremented. The prototype of this method is shown below:
public float get();
  1. The second variation of the get() method has a single parameter that represents a buffer index and returns the value of the buffer at this index. The prototype of this method is shown below:
public float get(int index);

Return value

The get() methods return a floating-point value.

If no parameters are passed to the get() method, it returns the current buffer position value. If the current buffer position exceeds the length of the buffer, the get() method throws a BufferUnderflowException.

If a buffer index is passed as a parameter to the get() method, it returns the value stored at the specified index. If the specified index is not a valid buffer position, the get() method throws an IndexOutOfBoundsException.

Example

The code below shows how the get() methods can be used in Java:

import java.nio.*;
import java.util.*;

class getMethods {
  public static void main(String[] args) {

    // initialize buffer instance
    FloatBuffer newBuffer = FloatBuffer.allocate(3);
    
    // add values to buffer
    newBuffer.put(3.2F);
    newBuffer.put(1.9F);
    newBuffer.put(5.8F);
    newBuffer.rewind();

    // Print buffer
    System.out.println("The FloatBuffer is: " + Arrays.toString(newBuffer.array()));
    
    // Using get() to return all buffer values
    System.out.println("\nUsing get() to return buffer values:");
    for(int i = 0; i < 3; i++)
    {
      float current_val = newBuffer.get();
      System.out.println(current_val);
    }

    // Use get(int index) to find value at position 1
    System.out.println("\nThe value at buffer index 1 is: ");
    System.out.println(newBuffer.get(1));
  }
}

Explanation

First, a FloatBuffer object called newBuffer is initialized through the allocate() method.

Since newBuffer has the capacity to store 33 elements, the put() method is invoked thrice to add values to the buffer. The rewind() function in line 1414 sets the current buffer position to the start of the buffer, i.e., index 00.

Next, a for-loop is used to extract each value stored in the buffer. Each loop iteration invokes the get() method in line 2323, which returns the value at the current buffer position and increments the current buffer position by 11.

Finally, the get() method is invoked with a specified buffer index in line 2929. The get() method returns the value of the buffer at index 11, i.e., 1.91.9.

RELATED TAGS

java

CONTRIBUTOR

Talha Ashar
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring