Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

What is the FloatBuffer limit() method in Java?

Khizar Hayat Saani

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.

FloatBuffer is a built-in class available in the java.nio.FloatBuffer package.

FloatBuffer implements a floating-point buffer and allows the user to perform various categories of operations upon buffers of datatype float.

limit() is a method defined in the FloatBuffer class. The following is the function prototype:

public final FloatBuffer limit(int newLimit)

Parameters and return value

The limit() method takes the following input parameters:

  • newLimit: A non-negative integer value that is set as the buffer’s new limit.

The method returns the updated FloatBuffer.

Note: Invoking the limit() method without any input parameters returns the current limit of the FloatBuffer.

Functionality

The limit() method changes the buffer’s limitThe maximum number of accessible elements in the buffer.

If we try to access an element beyond the buffer’s limit, it results in an exception, regardless of the available capacity.

When we create a FloatBuffer, the limit is set to its capacity by default. The limit() method changes this limit to the newLimit value passed as its input argument. The newLimit value must be non-negative and no larger than the buffer’s capacity.

If a markThe reset position of the buffer is already defined and is larger than the new specified limit, the newLimit is not set.

The limit() method

Code

import java.nio.*;
import java.util.*;
class Edpresso {
public static void main( String args[] ) {
// Creating a FloatBuffer
int capacity = 5;
FloatBuffer buff = FloatBuffer.allocate(capacity);
System.out.println("Capacity: " + capacity);
// Printing current limit
System.out.println("Default limit is: " + buff.limit());
// Populating buffer
buff.put(11.33F);
buff.put(53.10F);
// Setting new limit
System.out.println("Current position is: " + buff.position());
buff.limit(3);
System.out.println("New limit is: " + buff.limit());
// Printing buffer
System.out.println(Arrays.toString(buff.array()));
// Trying to access beyond limit by adding a 4th element
System.out.println("Current position is: " + buff.position());
buff.put(3, 63.03F);
}
}

In the above example, we begin by creating a FloatBuffer with a capacity of 5. At the time of creation, we can see that the limit is set to capacity.

Next, we add two elements. The limit() method is invoked and the buffer’s limit is set to 3.

Trying to add an element beyond the limit of the buffer throws an IndexOutOfBoundsException.

RELATED TAGS

java

CONTRIBUTOR

Khizar Hayat Saani
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