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:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 5 ;
try {
FloatBuffer fb = FloatBuffer.allocate(capacity);
fb.put( 8 .56F);
fb.put( 9 .61F);
fb.put( 1 .24F);
fb.rewind();
System.out.println( "Original FloatBuffer: "
+ Arrays.toString(fb.array()));
float value = fb.get();
System.out.println( "\nFloat Value: " + value);
float value1 = fb.get();
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:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
FloatBuffer fb = FloatBuffer.allocate(capacity);
fb.put( 8 .56F);
fb.put( 9 .61F);
System.out.println( "Original FloatBuffer: "
+ Arrays.toString(fb.array()));
float value = fb.get();
System.out.println( "\nFloat Value: " + value);
System.out.print( "\nsince the buffer current position is incremented" );
System.out.print( " to greater than its limit " );
float value1 = fb.get();
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:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
FloatBuffer fb = FloatBuffer.allocate(capacity);
fb.put( 8 .56F);
fb.put( 9 .61F);
fb.put( 6 .61F);
System.out.println( "Original FloatBuffer: "
+ Arrays.toString(fb.array()));
float value0 = fb.get( 0 );
System.out.println( "\nFloat Value at index 0: " + value0);
float value1 = fb.get( 1 );
System.out.println( "\nFloat Value at index 1: " + value1);
float value2 = fb.get( 2 );
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:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
FloatBuffer fb = FloatBuffer.allocate(capacity);
fb.put( 8 .56F);
fb.put( 9 .61F);
fb.put( 6 .61F);
System.out.println( "Original FloatBuffer: "
+ Arrays.toString(fb.array()));
float value0 = fb.get( 0 );
System.out.println( "\nFloat Value at index 0: " + value0);
float value1 = fb.get( 1 );
System.out.println( "\nFloat Value at index 1: " + value1);
System.out.println( "\nTrying to get the float"
+ " of index greater than its limit " );
float value2 = fb.get( 4 );
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...