FloatBuffer get() methods in Java with Examples
Last Updated :
24 May, 2019
get()
The
get() method of
java.nio.FloatBuffer Class is used to reads the float at the given buffer's current position, and then increments the position.
Syntax :
public abstract float get()
Return Value: This method returns the float value at the buffer's current position.
Throws: This method throws
BufferUnderflowException - If the buffer's current position is not smaller than its limit, then this exception is thrown.
Below are the examples to illustrate the get() method:
Examples 1:
Java
// Java program to demonstrate
// get() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the FloatBuffer
int capacity = 5;
// Creating the FloatBuffer
try {
// creating object of floatbuffer
// and allocating size capacity
FloatBuffer fb = FloatBuffer.allocate(capacity);
// putting the value in floatbuffer
fb.put(8.56F);
fb.put(9.61F);
fb.put(1.24F);
fb.rewind();
// print the FloatBuffer
System.out.println("Original FloatBuffer: "
+ Arrays.toString(fb.array()));
// Reads the float at this buffer's current position
// using get() method
float value = fb.get();
// print the Float value
System.out.println("\nFloat Value: " + value);
// Reads the float at this buffer's next position
// using get() method
float value1 = fb.get();
// print the Float value
System.out.print("\nNext Float Value: " + value1);
}
catch (IllegalArgumentException e) {
System.out.println("\nIllegalArgumentException catched");
}
catch (ReadOnlyBufferException e) {
System.out.println("\nReadOnlyBufferException catched");
}
catch (BufferUnderflowException e) {
System.out.println("\nException throws: " + e);
}
}
}
Output:
Original FloatBuffer: [8.56, 9.61, 1.24, 0.0, 0.0]
Float Value: 8.56
Next Float Value: 9.61
Examples 2:
Java
// Java program to demonstrate
// get() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the FloatBuffer
int capacity = 3;
// Creating the FloatBuffer
try {
// creating object of floatbuffer
// and allocating size capacity
FloatBuffer fb = FloatBuffer.allocate(capacity);
// putting the value in floatbuffer
fb.put(8.56F);
fb.put(9.61F);
// print the FloatBuffer
System.out.println("Original FloatBuffer: "
+ Arrays.toString(fb.array()));
// Reads the float at this buffer's current position
// using get() method
float value = fb.get();
// print the Float value
System.out.println("\nFloat Value: " + value);
// Reads the float at this buffer's next position
// using get() method
System.out.print("\nsince the buffer current position is incremented");
System.out.print(" to greater than its limit ");
float value1 = fb.get();
// print the Float value
System.out.print("\nNext Float Value: " + value1);
}
catch (IllegalArgumentException e) {
System.out.println("\nIllegalArgumentException catched");
}
catch (ReadOnlyBufferException e) {
System.out.println("\nReadOnlyBufferException catched");
}
catch (BufferUnderflowException e) {
System.out.println("\nException throws : " + e);
}
}
}
Output:
Original FloatBuffer: [8.56, 9.61, 0.0]
Float Value: 0.0
since the buffer current position is incremented to greater than its limit
Exception throws : java.nio.BufferUnderflowException
get(int index)
The
get(int index) method of FloatBuffer is used to read the article at a specified index.
Syntax :
public abstract float get(int index)
Parameters: This method takes
index (The index from which the float will be read) as a parameter.
Return Value: This method returns the
float value at the given index.
Exception: This method throws
IndexOutOfBoundsException. If index is negative or not smaller than the buffer's limit this exception is thrown.
Below are the examples to illustrate the get(int index) method:
Examples 1:
Java
// Java program to demonstrate
// get(int index) method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the FloatBuffer
int capacity = 3;
// Creating the FloatBuffer
try {
// creating object of floatbuffer
// and allocating size capacity
FloatBuffer fb = FloatBuffer.allocate(capacity);
// putting the value in floatbuffer
fb.put(8.56F);
fb.put(9.61F);
fb.put(6.61F);
// print the FloatBuffer
System.out.println("Original FloatBuffer: "
+ Arrays.toString(fb.array()));
// Reads the float at the index 0 of the floatbuffer
// using get() method
float value0 = fb.get(0);
// print the Float value
System.out.println("\nFloat Value at index 0: " + value0);
// Reads the float at the index 1 of the floatbuffer
// using get() method
float value1 = fb.get(1);
// print the Float value
System.out.println("\nFloat Value at index 1: " + value1);
// Reads the float at the index 2 of the floatbuffer
// using get() method
float value2 = fb.get(2);
// print the Float value
System.out.println("\nFloat Value at index 2: " + value2);
}
catch (IllegalArgumentException e) {
System.out.println("\nIllegalArgumentException catched");
}
catch (IndexOutOfBoundsException e) {
System.out.println("\nReadOnlyBufferException catched");
}
catch (BufferUnderflowException e) {
System.out.println("\nException throws : " + e);
}
}
}
Output:
Original FloatBuffer: [8.56, 9.61, 6.61]
Float Value at index 0: 8.56
Float Value at index 1: 9.61
Float Value at index 2: 6.61
Examples 2:
Java
// Java program to demonstrate
// get() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the FloatBuffer
int capacity = 3;
// Creating the FloatBuffer
try {
// creating object of floatbuffer
// and allocating size capacity
FloatBuffer fb = FloatBuffer.allocate(capacity);
// putting the value in floatbuffer
fb.put(8.56F);
fb.put(9.61F);
fb.put(6.61F);
// print the FloatBuffer
System.out.println("Original FloatBuffer: "
+ Arrays.toString(fb.array()));
// Reads the float at the index 0 of the floatbuffer
// using get() method
float value0 = fb.get(0);
// print the Float value
System.out.println("\nFloat Value at index 0: " + value0);
// Reads the float at the index 1 of the floatbuffer
// using get() method
float value1 = fb.get(1);
// print the Float value
System.out.println("\nFloat Value at index 1: " + value1);
// Reads the float at the index 2 of the floatbuffer
// using get() method
System.out.println("\nTrying to get the float"
+ " of index greater than its limit ");
float value2 = fb.get(4);
// print the Float value
System.out.println("\nFloat Value at index 2: " + value2);
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (IndexOutOfBoundsException e) {
System.out.println("Exception thrown: " + e);
}
catch (BufferUnderflowException e) {
System.out.println("Exception throws : " + e);
}
}
}
Output:
Original FloatBuffer: [8.56, 9.61, 6.61]
Float Value at index 0: 8.56
Float Value at index 1: 9.61
Trying to get the float of index greater than its limit
Exception thrown: java.lang.IndexOutOfBoundsException
Similar Reads
FloatBuffer put() methods in Java with Examples
put(float f) The put(float f) method of java.nio.FloatBuffer Class is used to write the given float into the newly created float buffer at the current position, and then increments the position. Syntax : public abstract FloatBuffer put(float f) Parameters: This method takes the float value f as a pa
6 min read
FloatBuffer flip() methods in Java with Examples
The flip() method of java.nio.FloatBuffer Class is used to flip this buffer. By flipping this buffer, it meant that the buffer will be trimmed to the current position and then the position will be changed to zero. During this process, if any mark is there on the buffer, then that mark will be automa
2 min read
FloatBuffer slice() method in Java with Examples
The slice() method of java.nio.FloatBuffer Class is used to creates a new float buffer whose content is a shared subsequence of the given buffer's content.The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, a
3 min read
FloatBuffer wrap() method in Java with Examples
wrap(float[] array) The wrap() method of java.nio.FloatBuffer Class is used to wraps a float array into a buffer. The new buffer will be backed by the given float array; that is, modifications to the buffer will cause the array to be modified and vice versa. The new buffer's capacity and limit will
4 min read
FloatBuffer equals() method in Java with Examples
The equals() method of java.nio.FloatBuffer Class is used to check whether or not the given buffer is equal to another object. Two float buffers are equal if, and only if, They have the same element type, They have the same number of remaining elements, and The two sequences of remaining elements, c
4 min read
FloatBuffer hasArray() method in Java with Examples
The hasArray() method of java.nio.FloatBuffer class is used to ensure whether or not the given buffer is backed by an accessible float array. It returns true if there is an accessible backing array to this buffer, else it returns false. If this method returns true, then the array() and arrayOffset()
2 min read
FloatBuffer compact() method in Java With Examples
The compact() method of java.nio.FloatBuffer Class is used to compact the given buffer. The values between the buffer's current position and its limit are copied to the beginning of the buffer. The buffer's position is then set to n+1 and its limit is set to its capacity. The buffer's position is se
3 min read
FloatBuffer allocate() method in Java With Examples
The allocate() method of java.nio.FloatBuffer Class is used to allocate a new float buffer next to the existing buffer. The new buffer's position will be zero. Its limit will be its capacity. Its mark will be undefined. And each of its elements will be initialized to zero. It will have a backing arr
2 min read
FloatBuffer compareTo() method in Java With Examples
The compareTo() method of java.nio.FloatBuffer class is used to compare one buffer to another. Two float buffers are compared by comparing their sequences of remaining elements lexicographically, without regard to the starting position of each sequence within its corresponding buffer. Pairs of float
4 min read
Field get() method in Java with Examples
The get() method of java.lang.reflect.Field used to get the value of the field object. If Field has a primitive type then the value of the field is automatically wrapped in an object. If the field is a static field, the argument of obj is ignored; it may be null Otherwise, the underlying field is an
3 min read