ByteBuffer asShortBuffer() in Java

asShortBuffer(): This method is available in java.nio.ByteBuffer class of Java.

Syntax:

ShortBuffer java.nio.ByteBuffer.asShortBuffer()

This method creates a view of this byte buffer as a short buffer. The new buffer's position will be zero, its capacity and its limit will be the number of bytes remaining in this buffer divided by two, its mark will be undefined, and its byte order will be that of the byte buffer at the moment the view is created.

Note: The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only.

Parameters: NA

Returns: A new short buffer.

Exceptions: NA

Approach

Java

import java.nio.ByteBuffer;

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

        byte array[] = { 1234 };
        ByteBuffer bb = ByteBuffer.wrap(array);

        System.out.println(bb.asShortBuffer());

    }
}

Output:

java.nio.ByteBufferAsShortBufferB[pos=0 lim=2 cap=2]

No comments:

Post a Comment