Open In App

FloatBuffer limit() Method in Java with Examples

Last Updated : 10 Sep, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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());
    }
}


Output:

FloatBuffer before setting buffer’s limit:
[20.5, 30.5, 0.0, 0.0]
Position: 2
Limit: 4

FloatBuffer 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());
    }
}


Output:

FloatBuffer before setting buffer’s limit:
[20.5, 30.5, 40.5, 0.0, 0.0]
Position: 3
Limit: 5

FloatBuffer 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-



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads