ShortBuffer in java, Use of toString() method of ShortBuffer class.


 

ShortBuffer in java, Use of toString() method of ShortBuffer class.

In this tutorial, you will see the use of toString() method of ShortBuffer class.

In this tutorial, you will see the use of toString() method of ShortBuffer class.

Use of toString() method of ShortBuffer class.

In this tutorial, we will see how to represent the state of short buffer in the form of string.

ShortBuffer API:

The java.nio.ShortBuffer class extends java.nio.Buffer class. It provides the following methods:

Return type Method Description
static ShortBuffer allocate( int capacity)  The allocate(..) method allocate a short buffer of given capacity. 
abstract ShortBuffer toString() The toString() method returns a string that  summarizing the state of buffer.

Code

import java.nio.*;
import java.nio.ShortBuffer;
public class StateString {
  public static final int index = 2;
  public static void main(String[] arg) {
    ShortBuffer shortBuff = ShortBuffer.allocate(1024);
    shortBuff.put((short2775);
    shortBuff.put((short7653);
    System.out.println("Position of short buffer : "
                            +shortBuff.position());
    System.out.println("Limit of short buffer : "
                             +shortBuff.limit());
    System.out.println("capacity of short buffer : "
                          +shortBuff.capacity
());
    System.out.println("String representation of"
                    +
" state of short buffer : ");
    System.out.println(shortBuff.toString());
  }
}

Output

C:\>java StateString
Position of short buffer : 2
Limit of short buffer : 1024
capacity of short buffer : 1024
String representation of state of short buffer :
java.nio.HeapShortBuffer[pos=2 lim=1024 cap=1024]

Download this code

Ads