Open In App

ByteBuffer toString() method in Java with Examples

Last Updated : 06 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The toString() method of ByteBuffer class is the inbuilt method used to returns a string representing the data contained by ByteBuffer Object. A new String object is created and initialized to get the character sequence from this ByteBuffer object and then String is returned by toString(). Subsequent changes to this sequence contained by Object do not affect the contents of the String.
Syntax: 

public abstract String toString()

Return Value: This method returns the String representing the data contained by ByteBuffer Object.
Below programs illustrate the ByteBuffer.toString() method:
Example 1: 

Java




// Java program to demonstrate
// toString() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
 
    public static void main(String[] args)
    {
 
        // Declaring the capacity of the ByteBuffer
        int capacity = 5;
 
        // creating object of ByteBuffer
        // and allocating size capacity
        ByteBuffer bb1 = ByteBuffer.allocate(capacity);
 
        // putting the value in ByteBuffer
        bb1.put((byte)10);
        bb1.put((byte)20);
 
        // print the ByteBuffer
        System.out.println("Original ByteBuffer: "
                           + Arrays.toString(bb1.array()));
 
        // Creating a shared subsequence buffer of given ByteBuffer
        // using toString() method
        String value = bb1.toString();
 
        // print the ByteBuffer
        System.out.println("\nstring representation of ByteBuffer:  "
                           + value);
    }
}


Output: 

Original ByteBuffer: [10, 20, 0, 0, 0]

string representation of ByteBuffer:  java.nio.HeapByteBuffer[pos=2 lim=5 cap=5]

 

Example 2:

Java




// Java program to demonstrate
// toString() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
 
    public static void main(String[] args)
    {
 
        // Declaring the capacity of the ByteBuffer
        int capacity = 4;
 
        // creating object of ByteBuffer
        // and allocating size capacity
        ByteBuffer bb1 = ByteBuffer.allocate(capacity);
 
        // putting the value in ByteBuffer
        bb1.put((byte)10)
            .put((byte)20)
            .put((byte)30)
            .put((byte)40);
 
        // print the ByteBuffer
        System.out.println("Original ByteBuffer: "
                           + Arrays.toString(bb1.array()));
 
        // Creating a shared subsequence buffer of given ByteBuffer
        // using toString() method
        String value = bb1.toString();
 
        // print the ByteBuffer
        System.out.println("\nstring representation of ByteBuffer:  "
                           + value);
    }
}


Output: 

Original ByteBuffer: [10, 20, 30, 40]

string representation of ByteBuffer:  java.nio.HeapByteBuffer[pos=4 lim=4 cap=4]

 

Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#toString–
 



Similar Reads

ByteBuffer compareTo() method in Java With Examples
The compareTo() method of java.nio.ByteBuffer class is used to compare one buffer to another. Two byte buffers are compared by comparing their sequences of remaining elements lexicographically, without regard to the starting position of each sequence within its corresponding buffer. Pairs of byte elements are compared as if by invoking Byte.compare
4 min read
ByteBuffer allocate() method in Java with Examples
The allocate() method of java.nio.ByteBuffer class is used to allocate a new byte buffer. The new buffer's position will be zero, its limit will be its capacity, its mark will be undefined, and each of its elements will be initialized to zero. It will have a backing array, and its array offset will be zero. Syntax : public static ByteBuffer allocat
2 min read
ByteBuffer asCharBuffer() method in Java with Examples
The asCharBuffer() method of java.nio.ByteBuffer class is used to create a view of this byte buffer as a char buffer.The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.The
3 min read
ByteBuffer array() method in Java with Examples
The array() method of java.nio.ByteBuffer class is used to return the byte array that backs the taken buffer.Modifications to this buffer's content will cause the returned array's content to be modified, and vice versa.Invoke the hasArray() method before invoking this method in order to ensure that this buffer has an accessible backing array. Synta
3 min read
ByteBuffer asFloatBuffer() method in Java with Examples
The asFloatBuffer() method of java.nio.ByteBuffer class is used to create view of this byte buffer as a float buffer.The content of the new buffer will start from this buffer's current position. Changes made to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independ
3 min read
ByteBuffer asDoubleBuffer() method in Java with Examples
The asDoubleBuffer() method of java.nio.ByteBuffer class is used to create a view of this byte buffer as a double buffer.The content of the new buffer will start from this buffer's current position. Changes made to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be inde
3 min read
ByteBuffer asIntBuffer() method in Java with Examples
The asIntBuffer() method of java.nio.ByteBuffer class is used to create a view of this byte buffer as an int buffer. The content of the new buffer will start from this buffer's current position. Changes made to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independ
3 min read
ByteBuffer arrayOffset() method in Java with Examples
The arrayOffset() method of java.nio.ByteBuffer class is used to return the offset within the given buffer's backing array of the first element of the buffer. If this buffer is backed by an array then buffer position p corresponds to array index p + arrayOffset(). Invoke the hasArray method before invoking this method in order to ensure that this b
2 min read
ByteBuffer asLongBuffer() method in Java with Examples
The asLongBuffer() method of java.nio.ByteBuffer class is used to create a view of this byte buffer as a long buffer.The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.The
3 min read
ByteBuffer asShortBuffer() method in Java with Examples
The asShortBuffer() method of java.nio.ByteBuffer class is used to create a view of this byte buffer as a short buffer.The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.T
3 min read
Practice Tags :