LongBuffer put(long) in Java

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

Syntax:

LongBuffer java.nio.LongBuffer.put(long l)

This method takes one argument of type long as its parameter. This method writes the given long into this buffer at the current position and then increments the position.

Parameters: One parameter is required for this method.

l: The long to be written.

Returns: This buffer.

Throws:

1. BufferOverflowException - If this buffer's current position is not smaller than its limit.

2. ReadOnlyBufferException - If this buffer is read-only.

Approach 1: When no exceptions.

Java

import java.nio.LongBuffer;

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

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

        long l = 1919;
        System.out.println(lb.put(l));
    }
}

Output:

java.nio.HeapLongBuffer[pos=1 lim=4 cap=4]


Approach 2: BufferOverflowException 

Java

import java.nio.LongBuffer;

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

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

        long l = 1919;
        System.out.println(lb.put(l));
    }
}


Output:

Exception in thread "main" java.nio.BufferOverflowException at java.base/java.nio.Buffer.nextPutIndex(Buffer.java:717) at java.base/java.nio.HeapLongBuffer.put(HeapLongBuffer.java:212)



Approach 3: ReadOnlyBufferException 

Java

import java.nio.LongBuffer;

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

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

        LongBuffer readOnly = lb.asReadOnlyBuffer();

        long l = 1919;
        System.out.println(readOnly.put(l));
    }
}


Output:

Exception in thread "main" java.nio.ReadOnlyBufferException at java.base/java.nio.HeapLongBufferR.put(HeapLongBufferR.java:215)


Some more put Methods.


put(long[])


put(LongBuffer)


put(int, long)


put(int, long[])


put(long[], int, int)


put(int, long[], int, int)


No comments:

Post a Comment