The limit() method of java.nio.FloatBuffer Class is used to modify this FloatBuffer’s limit. This method takes the limit to be set as the parameter and sets that as the new limit of this Buffer. If the mark of this Buffer is already defined and is larger than the new specified limit, then this new limit is not set and discarded.
Syntax:
public final FloatBuffer limit(int newLimit)
Parameter: The method takes one parameter newLimit of integer type which refers to the limit that is to be set as the new limit of the buffer.
Return Value: The method returns this buffer after setting the specified new limit as the new limit of this Buffer.
Below examples illustrate the limit() method:
Examples 1:
// Java program to demonstrate // limit() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // defining and allocating FloatBuffer // using allocate() method FloatBuffer floatBuffer = FloatBuffer.allocate(4); // put float value in FloatBuffer // using put() method floatBuffer.put(20.5f); floatBuffer.put(30.5f); // print the float buffer System.out.println( "FloatBuffer before " + "setting buffer's limit: " + Arrays.toString( floatBuffer.array()) + "\nPosition: " + floatBuffer.position() + "\nLimit: " + floatBuffer.limit()); // Limit the floatBuffer // using limit() method floatBuffer.limit(1); // print the float buffer System.out.println( "\nFloatBuffer after " + "setting buffer's limit: " + Arrays.toString( floatBuffer.array()) + "\nPosition: " + floatBuffer.position() + "\nLimit: " + floatBuffer.limit()); } } |
FloatBuffer before setting buffer’s limit:
[20.5, 30.5, 0.0, 0.0]
Position: 2
Limit: 4FloatBuffer after setting buffer’s limit:
[20.5, 30.5, 0.0, 0.0]
Position: 1
Limit: 1
Examples 2:
// Java program to demonstrate // limit() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // defining and allocating FloatBuffer // using allocate() method FloatBuffer floatBuffer = FloatBuffer.allocate(5); // put float value in FloatBuffer // using put() method floatBuffer.put(20.5f); floatBuffer.put(30.5f); floatBuffer.put(40.5f); // mark will be going to // discarded by limit() floatBuffer.mark(); // print the float buffer System.out.println( "FloatBuffer before " + "setting buffer's limit: " + Arrays.toString( floatBuffer.array()) + "\nPosition: " + floatBuffer.position() + "\nLimit: " + floatBuffer.limit()); // Limit the floatBuffer // using limit() method floatBuffer.limit(4); // print the double buffer System.out.println( "\nFloatBuffer before " + "setting buffer's limit: " + Arrays.toString( floatBuffer.array()) + "\nPosition: " + floatBuffer.position() + "\nLimit: " + floatBuffer.limit()); } } |
FloatBuffer before setting buffer’s limit:
[20.5, 30.5, 40.5, 0.0, 0.0]
Position: 3
Limit: 5FloatBuffer before setting buffer’s limit:
[20.5, 30.5, 40.5, 0.0, 0.0]
Position: 3
Limit: 4
Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/FloatBuffer.html#limit-int-
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- FloatBuffer duplicate() method in Java with Examples
- FloatBuffer hasArray() method in Java with Examples
- FloatBuffer compact() method in Java With Examples
- FloatBuffer asReadOnlyBuffer() method in Java with Examples
- FloatBuffer equals() method in Java with Examples
- FloatBuffer array() method in Java With Examples
- FloatBuffer slice() method in Java with Examples
- FloatBuffer wrap() method in Java with Examples
- FloatBuffer allocate() method in Java With Examples
- FloatBuffer arrayOffset() method in Java With Examples
- FloatBuffer compareTo() method in Java With Examples
- LongBuffer limit() method in Java with Examples
- IntBuffer limit() method in Java with Examples
- ShortBuffer limit() method in Java with Examples
- FloatBuffer get() methods in Java with Examples
- FloatBuffer put() methods in Java with Examples
- FloatBuffer mark() methods in Java with Examples
- FloatBuffer reset() methods in Java with Examples
- FloatBuffer clear() methods in Java with Examples
- FloatBuffer rewind() methods in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

