DoubleBuffer get() methods in Java with Examples
The get() method of java.nio.DoubleBuffer Class is used to reads the double at the given buffer’s current position, and then increments the position.
Syntax:
public abstract double get()
Return Value: This method returns the double value at the buffer’s current position.
Exception: 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 {
DoubleBuffer fb
= DoubleBuffer.allocate(capacity);
fb.put( 8 .56D);
fb.put( 9 .61D);
fb.put( 1 .24D);
fb.rewind();
System.out.println( "Original DoubleBuffer: "
+ Arrays.toString(fb.array()));
double value = fb.get();
System.out.println( "\nDouble Value: "
+ value);
double value1 = fb.get();
System.out.print( "\nNext double 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 DoubleBuffer: [8.56, 9.61, 1.24, 0.0, 0.0]
Double Value: 8.56
Next double Value: 9.61
Examples 2:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
DoubleBuffer fb = DoubleBuffer.allocate(capacity);
fb.put( 8 .56F);
fb.put( 9 .61F);
System.out.println( "Original DoubleBuffer: "
+ Arrays.toString(fb.array()));
Double value = fb.get();
System.out.println( "\nDouble Value: " + value);
System.out.print( "\nsince the buffer current"
+ " position is incremented" );
System.out.print( " to greater than its limit " );
Double value1 = fb.get();
System.out.print( "\nNext Double 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 DoubleBuffer: [8.5600004196167, 9.609999656677246, 0.0]
Double Value: 0.0
since the buffer current position is incremented to greater than its limit
Exception throws: java.nio.BufferUnderflowException
Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/DoubleBuffer.html#get–
Last Updated :
04 Oct, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...