ByteBuffer getDouble() method in Java with Examples
Last Updated :
17 Jun, 2019
getDouble()
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
// 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);
}
}
}
Output:
Original ByteBuffer:
1234.3456 2884.4444
Byte Value: 1234.3456
Next Byte Value: 2884.4444
Examples 2:
Java
// 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);
}
}
}
Output:
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--
getDouble(int index)
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
// 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);
}
}
}
Output:
Original ByteBuffer:
1234.3456 2884.4444
Byte Value: 1234.3456
Next Byte Value: 2884.4444
Examples 2:
Java
// 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);
}
}
}
Output:
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-
Similar Reads
ByteBuffer get() method in Java with Examples
get() The get() method of java.nio.ByteBuffer class is used to read the byte at the buffer's current position, and then increments the position. Syntax : public abstract byte get() Return Value: This method returns the byte at the buffer's current position. Throws: This method throws BufferUnderflow
6 min read
ByteBuffer getLong() method in Java with Examples
getLong() The getLong() method of java.nio.ByteBuffer class is used to read the next eight bytes at this buffer's current position, composing them into a long value according to the current byte order, and then increments the position by eight. Syntax: public abstract long getLong() Return Value: Th
5 min read
ByteBuffer getInt() method in Java with Examples
getInt() The getInt() method of java.nio.ByteBuffer class is used to read the next four bytes at this buffer's current position, composing them into an int value according to the current byte order, and then increments the position by four. Syntax: public abstract int getInt() Return Value: This met
5 min read
ByteBuffer getFloat() method in Java with Examples
getFloat() The getFloat() method of java.nio.ByteBuffer class is used to read the next four bytes at this buffer's current position, composing them into a float value according to the current byte order, and then increments the position by four. Syntax: public abstract float getFloat() Return Value:
5 min read
ByteBuffer getShort() method in Java with Examples
getShort() The getShort() method of java.nio.ByteBuffer class is used to read the next two bytes at this buffer's current position, composing them into a short value according to the current byte order, and then increments the position by two. Syntax: public abstract short getShort() Return Value: T
5 min read
ByteBuffer getChar() method in Java with Examples
getChar() The getChar() method of java.nio.ByteBuffer class is used to get method for reading a char value Reads the next two bytes at this buffer's current position, composing them into a char value according to the current byte order, and then increments the position by two. Syntax: public abstrac
6 min read
ByteBuffer flip() methods in Java with Examples
The flip() method of java.nio.ByteBuffer Class is used to flip this buffer. The limit is set to the current position and then the position is set to zero. If the mark is defined then it is discarded. After a sequence of channel-read or put operations, invoke this method to prepare for a sequence of
3 min read
ByteBuffer hashCode() method in Java with Examples
The hashCode() method of java.nio.ByteBuffer class is used to return the current hash code of this buffer. The hash code of a byte buffer depends only upon its remaining elements; that is, upon the elements from position() up to, and including, the element at limit() - 1. Because buffer hash codes a
2 min read
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 throw
3 min read
ByteBuffer wrap() method in Java with Examples
wrap(byte[] array) The wrap() method of java.nio.ByteBuffer Class is used to wraps a byte array into a buffer. The new buffer will be backed by the given byte 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 be a
4 min read