VOOZH about

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

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


  • Courses
  • Tutorials
  • Interview Prep

ByteBuffer getInt() method in Java with Examples

Last Updated : 17 Jun, 2019
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 method returns the int value at the buffer's current position Throws: This method throws BufferUnderflowException – If there are fewer than four bytes remaining in this buffer. Below are the examples to illustrate the getInt() method: Examples 1:
Output:
Original ByteBuffer: 
10 20 30 

Byte Value: 10
Next Byte Value: 20
Examples 2:
Output:
Original ByteBuffer: 
10 20 

Byte Value: 10
Next Byte Value: 20

there are fewer than four bytes remaining in this buffer
Exception Thrown : java.nio.BufferUnderflowException
Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#getInt--
getInt(int index)
The getInt(int index) method of ByteBuffer is used to read four bytes at the given index, composing them into a int value according to the current byte order. Syntax :
public abstract int getInt(int index)
Parameters: This method takes index (The index from which the Byte will be read) as a parameter. Return Value: This method returns The int 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 getInt(int index) method: Examples 1:
Output:
Original ByteBuffer: 
10 20 

Byte Value: 10
Next Byte Value: 20
Examples 2:
Output:
Original ByteBuffer: 
10 20 

Byte Value: 10

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