ByteBuffer getDouble() method in Java with Examples
The getDouble() method of java.nio.ByteBuffer class is used to read the next eight bytes at this buffer’s current position, composing them into a double value according to the current byte order, and then increments the position by eight.
Syntax:
public abstract double getDouble()
Return Value: This method returns the double 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 getDouble() method:
Examples 1:
// Java program to demonstrate // getDouble() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the ByteBuffer int capacity = 16; // Creating the ByteBuffer try { // creating object of ByteBuffer // and allocating size capacity ByteBuffer bb = ByteBuffer.allocate(capacity); // putting the double value in the bytebuffer bb.asDoubleBuffer() .put(1234.3456) .put(2884.4444); // rewind the Bytebuffer bb.rewind(); // print the ByteBuffer System.out.println("Original ByteBuffer: "); for (int i = 1; i <= capacity / 8; i++) System.out.print(bb.getDouble() + " "); // rewind the Bytebuffer bb.rewind(); // Reads the double at this buffer's current position // using getDouble() method double value = bb.getDouble(); // print the char value System.out.println("\n\nByte Value: " + value); // Reads the char at this buffer's next position // using getDouble() method double value1 = bb.getDouble(); // print the char value System.out.print("\nNext Byte Value: " + value1); } catch (BufferUnderflowException e) { System.out.println("\nException Thrown : " + e); } } } |
Original ByteBuffer: 1234.3456 2884.4444 Byte Value: 1234.3456 Next Byte Value: 2884.4444
Examples 2:
// Java program to demonstrate // getDouble() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the ByteBuffer int capacity = 16; // Creating the ByteBuffer try { // creating object of ByteBuffer // and allocating size capacity ByteBuffer bb = ByteBuffer.allocate(capacity); // putting the double value in the bytebuffer bb.asDoubleBuffer() .put(1234.3456) .put(2884.4444); // rewind the Bytebuffer bb.rewind(); // print the ByteBuffer System.out.println("Original ByteBuffer: "); for (int i = 1; i <= capacity / 8; i++) System.out.print(bb.getDouble() + " "); // rewind the Bytebuffer bb.rewind(); // Reads the double at this buffer's current position // using getDouble() method double value = bb.getDouble(); // print the char value System.out.println("\n\nByte Value: " + value); // Reads the char at this buffer's next position // using getDouble() method double value1 = bb.getDouble(); // print the char value System.out.println("\nNext Byte Value: " + value1); // Reads the char at this buffer's next position // using getDouble() method double value2 = bb.getDouble(); } catch (BufferUnderflowException e) { System.out.println("\nthere are fewer than " + "eight bytes remaining in" + " this buffer"); System.out.println("Exception Thrown : " + e); } } } |
Original ByteBuffer: 1234.3456 2884.4444 Byte Value: 1234.3456 Next Byte Value: 2884.4444 there are fewer than eight bytes remaining in this buffer Exception Thrown : java.nio.BufferUnderflowException
Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#getDouble–
The getDouble(int index) method of ByteBuffer is used to read eight bytes at the given index, composing them into a double value according to the current byte order.
Syntax :
public abstract double getDouble(int index)
Parameters: This method takes index as parameter which is the index from which the Byte will be read.
Return Value: This method returns The double 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 getDouble(int index) method:
Examples 1:
// Java program to demonstrate // getDouble() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the ByteBuffer int capacity = 16; // Creating the ByteBuffer try { // creating object of ByteBuffer // and allocating size capacity ByteBuffer bb = ByteBuffer.allocate(capacity); // putting the double value in the bytebuffer bb.asDoubleBuffer() .put(1234.3456) .put(2884.4444); // rewind the Bytebuffer bb.rewind(); // Declaring the variable double c; // print the ByteBuffer System.out.println("Original ByteBuffer: "); for (int i = 1; i <= capacity / 8; i++) System.out.print(bb.getDouble() + " "); // rewind the Bytebuffer bb.rewind(); // Reads the double at this buffer's current position // using getDouble() method double value = bb.getDouble(0); // print the char value System.out.println("\n\nByte Value: " + value); // Reads the char at this buffer's next position // using getDouble() method double value1 = bb.getDouble(8); // print the char value System.out.print("\nNext Byte Value: " + value1); } catch (IndexOutOfBoundsException e) { System.out.println("\nindex is negative or " + "smaller than the buffer's " + "limit, minus seven"); System.out.println("Exception Thrown : " + e); } } } |
Original ByteBuffer: 1234.3456 2884.4444 Byte Value: 1234.3456 Next Byte Value: 2884.4444
Examples 2:
// Java program to demonstrate // getDouble() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the ByteBuffer int capacity = 16; // Creating the ByteBuffer try { // creating object of ByteBuffer // and allocating size capacity ByteBuffer bb = ByteBuffer.allocate(capacity); // putting the double value in the bytebuffer bb.asDoubleBuffer() .put(1234.3456) .put(2884.4444); // rewind the Bytebuffer bb.rewind(); // Declaring the variable double c; // print the ByteBuffer System.out.println("Original ByteBuffer: "); for (int i = 1; i <= capacity / 8; i++) System.out.print(bb.getDouble() + " "); // rewind the Bytebuffer bb.rewind(); // Reads the double at this buffer's current position // using getDouble() method double value = bb.getDouble(0); // print the char value System.out.println("\n\nByte Value: " + value); // Reads the char at this buffer's next position // using getDouble() method double value1 = bb.getDouble(9); // print the char value System.out.print("\nNext Byte Value: " + value1); } catch (IndexOutOfBoundsException e) { System.out.println("\nindex is negative or" + " smaller than the buffer's" + " limit, minus seven"); System.out.println("Exception Thrown : " + e); } } } |
Original ByteBuffer: 1234.3456 2884.4444 Byte Value: 1234.3456 index is negative or smaller than the buffer's limit, minus seven Exception Thrown : java.lang.IndexOutOfBoundsException
Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#getDouble-int-
Recommended Posts:
- ByteBuffer get() method in Java with Examples
- ByteBuffer asIntBuffer() method in Java with Examples
- ByteBuffer compareTo() method in Java With Examples
- ByteBuffer asFloatBuffer() method in Java with Examples
- ByteBuffer compact() method in Java with Examples
- ByteBuffer asDoubleBuffer() method in Java with Examples
- ByteBuffer duplicate() method in Java with Examples
- ByteBuffer hashCode() method in Java with Examples
- ByteBuffer hasArray() method in Java with Examples
- ByteBuffer allocateDirect() method in Java with Examples
- ByteBuffer arrayOffset() method in Java with Examples
- ByteBuffer slice() method in Java with Examples
- ByteBuffer toString() method in Java with Examples
- ByteBuffer asReadOnlyBuffer() method in Java with Examples
- ByteBuffer wrap() method in Java with Examples
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.



