VOOZH about

URL: https://www.geeksforgeeks.org/java/bytebuffer-getfloat-method-in-java-with-examples/

⇱ ByteBuffer getFloat() method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

ByteBuffer getFloat() method in Java with Examples

Last Updated : 17 Jun, 2019
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: This method returns the float value at the buffer's current position. Exception: This method throws BufferUnderflowException if there are fewer than four bytes remaining in this buffer. Below are the examples to illustrate the getFloat() method: Examples 1:
Output:
Original ByteBuffer: 
12.3 28.44 

Byte Value: 12.3

Next Byte Value: 28.44
Examples 2:
Output:
Original ByteBuffer: 
12.3 28.44 

Byte Value: 12.3

Next Byte Value: 28.44

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#getFloat--
getFloat(int index)
The getFloat(int index) method of java.nio.ByteBuffer is used to read four bytes at the given index, composing them into a float value according to the current byte order. Syntax:
public abstract float getFloat(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 float value at the given index. Exception: This method throws IndexOutOfBoundsException if index is negative or not smaller than the buffer’s limit. Below are the examples to illustrate the getFloat(int index) method: Examples 1:
Output:
Original ByteBuffer: 
12.3 28.44 

Byte Value: 12.3

Next Byte Value: 28.44
Examples 2:
Output:
Original ByteBuffer: 
12.3 28.44 

Byte Value: 12.3

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#getFloat-int-
Comment