ShortBuffer class in java

java.nio.ShortBuffer is a class in java programming language. Inside of this class exists 25 public methods. Declaration of this class as follows.

public abstract class ShortBuffer
extends Buffer
implements Comparable<ShortBuffer>

ShortBuffer public method

There are following useful methods which is define the inside of java.nio.ShortBuffer class.

MethodDescription
static ShortBuffer allocate(int capacity) It allocates a new short buffer.
short[] array() It returns the short array that backs this buffer (optional operation).
int arrayOffset() It returns the offset within this buffer's backing array of the first element of the buffer (optional operation).
abstract ShortBuffer asReadOnlyBuffer() It creates a new, read-only short buffer that shares this buffer's content.
abstract ShortBuffer compact() It compacts this buffer (optional operation).
int compareTo(ShortBuffer that) It compares this buffer to another.
abstract ShortBuffer duplicate() It creates a new short buffer that shares this buffer's content.
boolean equals(Object ob) It tells whether or not this buffer is equal to another object.
abstract short get() It relative get method.
abstract short get(int index) It absolute get method.
ShortBuffer get(short[] dst) It relative bulk get method.
ShortBuffer get(short[] dst, int offset, int length) It relative bulk get method.
boolean hasArray() It tells whether or not this buffer is backed by an accessible short array.
int hashCode() It returns the current hash code of this buffer.
abstract boolean isDirect() It tells whether or not this short buffer is direct.
abstract ByteOrder order() It retrieves this buffer's byte order.
abstract ShortBuffer put(int index, short s) It absolute put method (optional operation).
abstract ShortBuffer put(short s) It relative put method (optional operation).
ShortBuffer put(short[] src) It relative bulk put method (optional operation).
ShortBuffer put(short[] src, int offset, int length) It relative bulk put method (optional operation).
ShortBuffer put(ShortBuffer src) It relative bulk put method (optional operation).
abstract ShortBuffer slice() It creates a new short buffer whose content is a shared subsequence of this buffer's content.
String toString() It returns a string summarizing the state of this buffer.
static ShortBuffer wrap(short[] array) It wraps a short array into a buffer.
static ShortBuffer wrap(short[] array, int offset, int length) It wraps a short array into a buffer.

This reference is belong to javadoc

java.nio.ShortBuffer allocate(int) method example

import java.nio.ShortBuffer;
public class Example
{
    public static void main(String[] args)
    {
        try
        {
            ShortBuffer buffer = ShortBuffer.allocate(5);
            // Put short value
            buffer.put((short) 14);
            buffer.put((short) 32);
            buffer.put((short) 23);
            buffer.put((short) 45);
            buffer.put((short) 34);
            buffer.rewind();
            System.out.println(buffer);
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }
}
Java ShortBuffer class implementation
java.nio.HeapShortBuffer[pos=0 lim=5 cap=5]

short[] array() method example

// Java program for java.nio.ShortBuffer class array() method
// Useful packages
import java.nio.ShortBuffer;
public class Example
{
    public static void main(String[] args)
    {
        try
        {
            ShortBuffer buffer = ShortBuffer.allocate(5);
            // Put short value
            buffer.put((short) 14);
            buffer.put((short) 32);
            buffer.put((short) 23);
            buffer.put((short) 45);
            buffer.put((short) 34);
            buffer.rewind();
            System.out.println(buffer);
            // Get short array
            short[] result = buffer.array();
            for (short value: result)
            {
                System.out.println(value);
            }
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }
}
java.nio.HeapShortBuffer[pos=0 lim=5 cap=5]
14
32
23
45
34

java.nio.ShortBuffer arrayOffset() method example

// Java program for java.nio.ShortBuffer class arrayOffset() method
// Useful packages
import java.nio.ShortBuffer;
class Example
{
    public static void main(String[] args)
    {
        ShortBuffer buffer = ShortBuffer.allocate(5);
        // Put short value
        buffer.put((short) 10);
        buffer.put((short) 20);
        buffer.put((short) 30);
        buffer.rewind();
        int result = buffer.arrayOffset();
        // Display result 
        System.out.println(result);
    }
}
0

java.nio.ShortBuffer asReadOnlyBuffer() method example

// Java program for java.nio.ShortBuffer class, asReadOnlyBuffer() method
// Useful packages
import java.nio.ShortBuffer;
class Example {
    public static void main(String[] args) {
        
        
        ShortBuffer buffer = ShortBuffer.allocate(5);
        // Put short value
        buffer.put((short) 10);
        buffer.put((short) 20);
        buffer.put(2,(short) 30);
        buffer.rewind();
      
        ShortBuffer read = buffer.asReadOnlyBuffer();
        // Value are not change
        // read.put(2,(short) 30);

        // Display read buffer 
        while (read.hasRemaining()){
            System.out.println(read.get());
        } 
    }
}
asReadOnlyBuffer method in Java ShortBuffer class
10
20
30
0
0

java.nio.ShortBuffer compact() method example

// Java program for java.nio.ShortBuffer class compact() method
// Useful packages
import java.nio.ShortBuffer;
import java.util.Arrays;
class Example
{
    public static void main(String[] args)
    {
        ShortBuffer buffer = ShortBuffer.allocate(6);
        // Put short value
        buffer.put((short) 10);
        buffer.put((short) 20);
        buffer.put((short) 30);

        System.out.println(Arrays.toString(buffer.array()));
        ShortBuffer compactBuffer = buffer.compact();
        System.out.println(Arrays.toString(compactBuffer.array()));
        // Update value
        compactBuffer.put(4,(short) 50);
        System.out.println(Arrays.toString(compactBuffer.array()));

    }
}
[10, 20, 30, 0, 0, 0]
[0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 50, 0]

java.nio.ShortBuffer compareTo(ShortBuffer) method example

// Java program for java.nio.ShortBuffer class compact() method
// Useful packages
import java.nio.ShortBuffer;
import java.util.Arrays;
class Example
{
    public static void main(String[] args)
    {
        ShortBuffer b1 = ShortBuffer.allocate(5);
        ShortBuffer b2 = ShortBuffer.allocate(5);
        ShortBuffer b3 = ShortBuffer.allocate(6);
        // Put short value in b1
        b1.put((short) 10);
        b1.put((short) 20);
        b1.put((short) 30);
        b1.rewind();
        // Put short value in b2
        b2.put((short) 10);
        b2.put((short) 20);
        b2.put((short) 30);
        b2.rewind();
        // Put short value in b3
        b3.put((short) 31);
        b3.put((short) 44);
        b3.put((short) 34);
        b3.rewind();
        // lexicographically equal 
        System.out.println(b1.compareTo(b2));
        // b2 lexicographically less of b3
        System.out.println(b2.compareTo(b3));
        // b3 lexicographically greater of b1
        System.out.println(b3.compareTo(b1));
    }
}
0
-21
21

java.nio.ShortBuffer duplicate() method example

// Java program for java.nio.ShortBuffer class duplicate() method
// Useful packages
import java.nio.ShortBuffer;
import java.util.Arrays;
class Example {
    public static void main(String[] args) {
        
        ShortBuffer b1 = ShortBuffer.allocate(5);

        // Put short value in b1
        b1.put((short) 10);
        b1.put((short) 20);
        b1.put((short) 30);
        b1.rewind();
        ShortBuffer b2 = b1.duplicate();

        System.out.println(Arrays.toString(b1.array()));
        System.out.println(Arrays.toString(b2.array()));
        // Change b1 value 
        b1.put(4,(short)100);

        System.out.println(Arrays.toString(b1.array()));
        System.out.println(Arrays.toString(b2.array()));
    }
}
[10, 20, 30, 0, 0]
[10, 20, 30, 0, 0]
[10, 20, 30, 0, 100]
[10, 20, 30, 0, 100]

java.nio.ShortBuffer equals(Object) method example

// Java program for java.nio.ShortBuffer class equals() method
// Useful packages
import java.nio.ShortBuffer;
import java.util.Arrays;
class Example
{
    public static void main(String[] args)
    {
        ShortBuffer b1 = ShortBuffer.allocate(5);
        ShortBuffer b2 = ShortBuffer.allocate(5);
        ShortBuffer b3 = ShortBuffer.allocate(6);
        // Put short value in b1
        b1.put((short) 10);
        b1.put((short) 20);
        b1.put((short) 30);
        b1.rewind();
        // Put short value in b2
        b2.put((short) 10);
        b2.put((short) 20);
        b2.put((short) 30);
        b2.rewind();
        // Put short value in b3
        b3.put((short) 31);
        b3.put((short) 44);
        b3.put((short) 34);
        b3.rewind();
        System.out.println(b1.equals(b2));
        System.out.println(b2.equals(b3));
        System.out.println(b3.equals(b1));
        System.out.println(b3.equals(b3));
    }
}
true
false
false
true


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved