Friday, July 5, 2024
HomeLanguagesJavaLongBuffer limit() method in Java with Examples

LongBuffer limit() method in Java with Examples

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


Output:

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

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


Output:

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

LongBuffer 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/LongBuffer.html#rewind–

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments