What is the doubleBuffer limit() method in Java?

The limit() method is part of the class java.nio.DoubleBuffer. This method sets the new limit of the buffer. It takes one parameter: an integral value of the new limit we set.

Syntax:

doubleBuffer.limit(newLimit )

Parameters

newLimit: The integer value of the index that is set as the new limit.

Return value

This method returns the double buffer instance with the updated limit.

Code

Let’s look at a coding example for this method.

import java.nio.*;
import java.util.*;
class HelloWorld {
public static void main( String args[] ) {
// initializing an double buffer by allocating memory
DoubleBuffer buff = DoubleBuffer.allocate(5);
// put values in doubleBuffer using put() method
buff.put(23.2);
buff.put(21.7);
// printing the buffer, we can see position pos = 2
// and limit lim = 5
System.out.println("Buffer before applying limit: ");
System.out.println(buff);
// print the buffer array
System.out.println(Arrays.toString(buff.array()));
// applying limit using limit() method
buff.limit(1);
// printing the buffer, we can now see that position pos = 1
// and limit lim = 1
System.out.println("Buffer after applying limit of 1: ");
System.out.println(buff);
// print the buffer array
System.out.println(Arrays.toString(buff.array()));
}
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved