Skip to content
Related Articles

Related Articles

IntBuffer limit() method in Java with Examples

View Discussion
Improve Article
Save Article
  • Last Updated : 26 Aug, 2019

The limit() method of java.nio.IntBuffer Class is used to modify this IntBuffer’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 IntBuffer 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: 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 IntBuffer
        // using allocate() method
        IntBuffer intBuffer
            = IntBuffer.allocate(4);
  
        // put int value in IntBuffer
        // using put() method
        intBuffer.put(20);
        intBuffer.put(30);
  
        // print the int buffer
        System.out.println(
            "IntBuffer before "
            + "setting buffer's limit: "
            + Arrays.toString(
                  intBuffer.array())
            + "\nPosition: "
            + intBuffer.position()
            + "\nLimit: "
            + intBuffer.limit());
  
        // Limit the intBuffer
        // using limit() method
        intBuffer.limit(1);
  
        // print the int buffer
        System.out.println(
            "\nintBuffer after "
            + "setting buffer's limit: "
            + Arrays.toString(
                  intBuffer.array())
            + "\nPosition: "
            + intBuffer.position()
            + "\nLimit: "
            + intBuffer.limit());
    }
}

Output:

IntBuffer before setting buffer's limit: [20, 30, 0, 0]
Position: 2
Limit: 4

intBuffer 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 IntBuffer
        // using allocate() method
        IntBuffer intBuffer
            = IntBuffer.allocate(5);
  
        // put int value in IntBuffer
        // using put() method
        intBuffer.put(20);
        intBuffer.put(30);
        intBuffer.put(40);
  
        // mark will be going to
        // discarded by limit()
        intBuffer.mark();
  
        // print the int buffer
        System.out.println(
            "intBuffer before "
            + "setting buffer's limit: "
            + Arrays.toString(
                  intBuffer.array())
            + "\nPosition: "
            + intBuffer.position()
            + "\nLimit: "
            + intBuffer.limit());
  
        // Limit the intBuffer
        // using limit() method
        intBuffer.limit(4);
  
        // print the int buffer
        System.out.println(
            "\nintBuffer before "
            + "setting buffer's limit: "
            + Arrays.toString(
                  intBuffer.array())
            + "\nPosition: "
            + intBuffer.position()
            + "\nLimit: "
            + intBuffer.limit());
    }
}

Output:

intBuffer before setting buffer's limit: [20, 30, 40, 0, 0]
Position: 3
Limit: 5

intBuffer 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/IntBuffer.html#limit-int-


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!