LongBuffer get() in Java

get(): This method is available in java.nio.LongBuffer class of Java.

Syntax:

long java.nio.LongBuffer.get()

This method reads the long at this buffer's current position and then increments the position. 

Parameters: NA

Returns: The long at the buffer's current position.

Throws:

BufferUnderflowException - If the buffer's current position is not smaller than its limit

Approach 1: When no exceptions.

Java

import java.nio.LongBuffer;

public class LongBufferget {
    public static void main(String[] args) {

        long array[] = { 1, 2, 3, 4 };
        LongBuffer lb = LongBuffer.wrap(array);

        System.out.println(lb.get());
    }
}

Output:

1


Approach 2: BufferUnderflowException 

Java

import java.nio.LongBuffer;

public class LongBufferget {
    public static void main(String[] args) {

        long array[] = {};
        LongBuffer lb = LongBuffer.wrap(array);

        System.out.println(lb.get());
    }
}


Output:

Exception in thread "main" java.nio.BufferUnderflowException at java.base/java.nio.Buffer.nextGetIndex(Buffer.java:694) at java.base/java.nio.HeapLongBuffer.get(HeapLongBuffer.java:166)


No comments:

Post a Comment