ByteBuffer toString() method in Java with Examples Last Updated : 06 Aug, 2021 Summarize Comments Improve Suggest changes Share Like Article Like 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-- Comment More infoAdvertise with us Next Article ByteBuffer putInt() methods in Java with Examples R rohitprasad3 Follow Improve Article Tags : Java Java-Functions Java-NIO package Java-ByteBuffer Practice Tags : Java Similar Reads Byte toString() method in Java with examples The toString() method of Byte class is a built in method in Java which is used to return a String value. public String toString() Syntax: ByteObject.toString() Return Value: It returns a String object, the value of which is equal to the value of the ByteObject. Below is the implementation of toStrin 2 min read ByteBuffer putLong() methods in Java with Examples putLong(int value) The putLong(int value) method of java.nio.ByteBuffer Class is used to write eight bytes containing the given long value, in the current byte order, into this buffer at the current position, and then increments the position by eight. Syntax: public abstract ByteBuffer putLong(long 6 min read ByteBuffer putInt() methods in Java with Examples putInt(int value) The putInt(int value) method of java.nio.ByteBuffer Class is used to write four bytes containing the given int value, in the current byte order, into this buffer at the current position, and then increments the position by four. Syntax : public abstract ByteBuffer putInt(int value) 6 min read ByteBuffer slice() method in Java with Examples The slice() method of java.nio.ByteBuffer Class is used to creates a new byte buffer whose content is a shared subsequence of the given buffer's content. 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, an 3 min read ByteBuffer putShort() methods in Java with Examples putShort(int value) The putShort(int value) method of java.nio.ByteBuffer Class is used to write two bytes containing the given short value, in the current byte order, into this buffer at the current position, and then increments the position by two. Syntax: public abstract ByteBuffer putShort(short 6 min read ByteBuffer putFloat() methods in Java with Examples putFloat(float value) The putFloat(float value) method of java.nio.ByteBuffer Class is used to write four bytes containing the given float value, in the current byte order, into this buffer at the current position, and then increments the position by four. Syntax: public abstract ByteBuffer putFloat 6 min read Like