Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

limit
java

What is the doubleBuffer limit() method in Java?

Sarvech Qadir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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()));
}
}

RELATED TAGS

limit
java

CONTRIBUTOR

Sarvech Qadir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring