ShortBuffer limit() method in Java with Examples Last Updated : 28 Aug, 2019 Comments Improve Suggest changes Like Article Like Report 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 // 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()); } } Output: 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 // 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()); } } Output: 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-- Comment More infoAdvertise with us Next Article ShortBuffer limit() method in Java with Examples rohitprasad3 Follow Improve Article Tags : Java Java-Functions Java-ShortBuffer Java-NIO package Practice Tags : Java Similar Reads ShortBuffer flip() methods in Java with Examples The flip() method of java.nio.ShortBuffer Class is used to flip this buffer. By flipping this buffer, it meant that the buffer will be trimmed to the current position and the then the position will be changed to zero. During this process, if any mark is there on the buffer, then that mark will be au 2 min read ShortBuffer array() Method in Java with Examples The array() method of java.nio.ShortBuffer is used to return the short array that backs this buffer(optional). Modifications to this buffer's content will cause the returned array's content to be modified, and vice versa. Invoke the hasArray method before invoking this method in order to ensure that 3 min read ShortBuffer rewind() method in Java with Examples The rewind() method of java.nio.ShortBuffer Class is used to rewind this buffer. By rewinding this Buffer, the following actions are taken: Current position is set to zero the mark is discarded, if any, but the mark value is unchanged. Syntax: public ShortBuffer rewind() Parameter: This method do no 2 min read ShortBuffer compact() method in Java With Examples The compact() method of java.nio.ShortBuffer Class is used to compact the given buffer. The shorts between the buffer's current position and its limit, if any, are copied to the beginning of the buffer. That is, the short at index p = position() is copied to index zero, the short at index p + 1 is c 3 min read ShortBuffer allocate() method in Java With Examples The allocate() method of java.nio.ShortBuffer Class is used to allocate a new short buffer.The position of the new Buffer will be zero & it's limit is its capacity, though the mark is undefined, and each of its elements is initialized to zero. It will be having a backing array, and the array off 2 min read ShortBuffer toString() method in Java with Examples The toString() method in java.nio.ShortBuffer is used to return a string summarizing the state of this buffer. Syntax: public String toString() Return Value:The method returns a summary string. Below are the examples to illustrate the toString() method: Program 1: Java // Java program to demonstrate 1 min read ShortBuffer compareTo method in Java with Examples The compareTo() method of java.nio.ShortBuffer class is used to compare one buffer to another. Two short buffers are compared by comparing their sequences of remaining elements lexicographically, without regard to the starting position of each sequence within its corresponding buffer. Pairs of short 4 min read ShortBuffer duplicate() method in Java With Examples The duplicate() method of java.nio.ShortBuffer Class creates a new short buffer that shares this buffer's content. The content of the new buffer will be that of this buffer. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark 3 min read ShortBuffer arrayOffset() method in Java With Examples The arrayOffset() method of java.nio.ShortBuffer class is used to return the offset within the bufferâs backing array of the first element of the buffer. It means that if this buffer is backed by an array, then buffer position p corresponds to array index p + arrayOffset(). In-order to check whether 3 min read Buffer limit() methods in Java with Examples The limit() method of java.nio.Buffer Class is used to set this buffer's limit. If the position is larger than the new limit then it is set to the new limit. If the mark is defined and larger than the new limit then it is discarded. Syntax: public Buffer limit(int newLimit) Return Value: This method 2 min read Like