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:
// 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 DoubleBuffer fb = DoubleBuffer.allocate(capacity); // putting the value in Doublebuffer fb.put(8.56D); fb.put(9.61D); fb.put(1.24D); fb.rewind(); // print the DoubleBuffer System.out.println("Original DoubleBuffer: " + Arrays.toString(fb.array())); // Reads the double at this buffer's current position // using get() method double value = fb.get(); // print the double value System.out.println("\nDouble Value: " + value); // Reads the double at this buffer's next position // using get() method double value1 = fb.get(); // print the double value 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); } } } |
Original DoubleBuffer: [8.56, 9.61, 1.24, 0.0, 0.0] Double Value: 8.56 Next double Value: 9.61
Examples 2:
// 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 DoubleBuffer int capacity = 3; // Creating the DoubleBuffer try { // creating object of Doublebuffer // and allocating size capacity DoubleBuffer fb = DoubleBuffer.allocate(capacity); // putting the value in Doublebuffer fb.put(8.56F); fb.put(9.61F); // print the DoubleBuffer System.out.println("Original DoubleBuffer: " + Arrays.toString(fb.array())); // Reads the Double at this buffer's current position // using get() method Double value = fb.get(); // print the Double value System.out.println("\nDouble Value: " + value); // Reads the Double 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 "); Double value1 = fb.get(); // print the Double value 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); } } } |
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–
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- DoubleBuffer put() methods in Java with Examples | Set 1
- DoubleBuffer flip() methods in Java with Examples
- DoubleBuffer clear() methods in Java with Examples
- DoubleBuffer mark() methods in Java with Examples
- DoubleBuffer limit() methods in Java with Examples
- DoubleBuffer order() methods in Java with Examples
- DoubleBuffer reset() methods in Java with Examples
- DoubleBuffer arrayOffset() method in Java With Examples
- DoubleBuffer compareTo() method in Java With Examples
- DoubleBuffer wrap() method in Java with Examples
- DoubleBuffer slice() method in Java with Examples
- DoubleBuffer asReadOnlyBuffer() method in Java with Examples
- DoubleBuffer equals() method in Java with Examples
- DoubleBuffer array() method in Java With Examples
- DoubleBuffer duplicate() method in Java with Examples
- DoubleBuffer allocate() method in Java With Examples
- DoubleBuffer compact() method in Java With Examples
- DoubleBuffer hasArray() method in Java with Examples
- DoubleBuffer rewind() Method in Java with Examples
- Static methods vs Instance methods in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : ManasChhabra2

