get(int): This method is available in java.nio.ByteBuffer class of Java.
Syntax:
byte java.nio.ByteBuffer.get(int index)
This method takes one argument of type int as its parameter. This method reads the byte at the given index.
Parameters: One parameter is required for this method.
index: The index from which the byte will be read.
Returns: The byte at the given index.
Throws:
IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit
Approach 1: When no exceptions.
Java
import java.nio.ByteBuffer;public class ByteBufferget3 {public static void main(String[] args) {byte array[] = { 1, 2, 3, 4 };ByteBuffer bb = ByteBuffer.wrap(array);int index = 2;System.out.println(bb.get(index));}}
Output:
3
Approach 2: IndexOutOfBoundsException
Java
import java.nio.ByteBuffer;public class ByteBufferget3 {public static void main(String[] args) {byte array[] = { 1, 2, 3, 4 };ByteBuffer bb = ByteBuffer.wrap(array);int index = -1;System.out.println(bb.get(index));}}jkda jk
Output:
Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.nio.Buffer.checkIndex(Buffer.java:738) at java.base/java.nio.HeapByteBuffer.get(HeapByteBuffer.java:171)
No comments:
Post a Comment