Saturday, October 18, 2025
HomeLanguagesJavaShortBuffer toString() method in Java with Examples

ShortBuffer toString() method in Java with Examples

The toString() method in java.nio.ShortBuffer is used to return a string summarizing the state of this buffer. Syntax:

public String toString()

Return Value:The method returns a summary string. Below are the examples to illustrate the toString() method: Program 1

Java




// Java program to demonstrate
// toString() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
 
    public static void main(String[] args)
    {
        // creating Shortbuffer
        ShortBuffer bb = ShortBuffer.allocate(100);
        bb.put((short)20);
        bb.put((short)30);
        // using toString method
        System.out.println(bb.toString());
    }
}


Output:

java.nio.HeapShortBuffer[pos=2 lim=100 cap=100]

Program 2

Java




// Java program to demonstrate
// toString() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
 
    public static void main(String[] args)
    {
        // creating Shortbuffer
        ShortBuffer bb = ShortBuffer.allocate(300);
        bb.put((short)20);
        bb.put((short)30);
        bb.rewind();
        // using toString method
        System.out.println(bb.toString());
    }
}


Output:

java.nio.HeapShortBuffer[pos=0 lim=300 cap=300]
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS