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.
doubleBuffer.limit(newLimit )
newLimit
: The integer value of the index that is set as the new limit.
This method returns the double buffer instance with the updated limit.
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 memoryDoubleBuffer buff = DoubleBuffer.allocate(5);// put values in doubleBuffer using put() methodbuff.put(23.2);buff.put(21.7);// printing the buffer, we can see position pos = 2// and limit lim = 5System.out.println("Buffer before applying limit: ");System.out.println(buff);// print the buffer arraySystem.out.println(Arrays.toString(buff.array()));// applying limit using limit() methodbuff.limit(1);// printing the buffer, we can now see that position pos = 1// and limit lim = 1System.out.println("Buffer after applying limit of 1: ");System.out.println(buff);// print the buffer arraySystem.out.println(Arrays.toString(buff.array()));}}
Free Resources