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:
Java
Java
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:
Examples 2:
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
// 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:
Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/FloatBuffer.html#limit-int-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