The limit() method of java.nio.ShortBuffer Class is used to modify this ShortBuffer’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 ShortBuffer limit(int newLimit)
Parameter: This method accepts a parameter newLimit which is the new integer value for the limit.
Return Value: This method returns this buffer after setting the specified new limit as the new limit of this Buffer.
Below are the examples to 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 ShortBuffer // using allocate() method ShortBuffer shortBuffer = ShortBuffer.allocate(4); // put short value in ShortBuffer // using put() method shortBuffer.put((short)20); shortBuffer.put((short)30); // print the short buffer System.out.println( "ShortBuffer before " + "setting buffer's limit: " + Arrays.toString( shortBuffer.array()) + "\nPosition: " + shortBuffer.position() + "\nLimit: " + shortBuffer.limit()); // Limit the shortBuffer // using limit() method shortBuffer.limit(1); // print the short buffer System.out.println( "\nShortBuffer after " + "setting buffer's limit: " + Arrays.toString( shortBuffer.array()) + "\nPosition: " + shortBuffer.position() + "\nLimit: " + shortBuffer.limit()); } } |
ShortBuffer before setting buffer's limit: [20, 30, 0, 0] Position: 2 Limit: 4 ShortBuffer after setting buffer's limit: [20, 30, 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 ShortBuffer // using allocate() method ShortBuffer shortBuffer = ShortBuffer.allocate(5); // put short value in ShortBuffer // using put() method shortBuffer.put((short)20); shortBuffer.put((short)30); shortBuffer.put((short)40); // mark will be going to // discarded by limit() shortBuffer.mark(); // print the short buffer System.out.println( "ShortBuffer before " + "setting buffer's limit: " + Arrays.toString( shortBuffer.array()) + "\nPosition: " + shortBuffer.position() + "\nLimit: " + shortBuffer.limit()); // Limit the shortBuffer // using limit() method shortBuffer.limit(4); // print the short buffer System.out.println( "\nShortBuffer before " + "setting buffer's limit: " + Arrays.toString( shortBuffer.array()) + "\nPosition: " + shortBuffer.position() + "\nLimit: " + shortBuffer.limit()); } } |
ShortBuffer before setting buffer's limit: [20, 30, 40, 0, 0] Position: 3 Limit: 5 ShortBuffer before setting buffer's limit: [20, 30, 40, 0, 0] Position: 3 Limit: 4
Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/ShortBuffer.html#mark–
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:
- ShortBuffer rewind() method in Java with Examples
- ShortBuffer compareTo method in Java with Examples
- ShortBuffer array() Method in Java with Examples
- ShortBuffer arrayOffset() method in Java With Examples
- ShortBuffer compact() method in Java With Examples
- ShortBuffer toString() method in Java with Examples
- ShortBuffer reset() method in Java with Examples
- ShortBuffer mark() method in Java with Examples
- ShortBuffer hasArray() method in Java with Examples
- ShortBuffer duplicate() method in Java With Examples
- ShortBuffer equals() method in Java with Examples
- ShortBuffer allocate() method in Java With Examples
- ShortBuffer asReadOnlyBuffer() method in Java with Examples
- ShortBuffer isDirect() Method in Java with Examples
- ShortBuffer hashCode() Method in Java with Examples
- ShortBuffer put(int, short) method in Java With Examples
- ShortBuffer order() Method in Java with Examples
- ShortBuffer slice() method in Java with Examples
- IntBuffer limit() method in Java with Examples
- FloatBuffer limit() Method 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.

