FloatBuffer limit() Method in Java with Examples
Last Updated :
10 Sep, 2019
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:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
FloatBuffer floatBuffer
= FloatBuffer.allocate( 4 );
floatBuffer.put( 20 .5f);
floatBuffer.put( 30 .5f);
System.out.println(
"FloatBuffer before "
+ "setting buffer's limit: "
+ Arrays.toString(
floatBuffer.array())
+ "\nPosition: "
+ floatBuffer.position()
+ "\nLimit: "
+ floatBuffer.limit());
floatBuffer.limit( 1 );
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:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
FloatBuffer floatBuffer
= FloatBuffer.allocate( 5 );
floatBuffer.put( 20 .5f);
floatBuffer.put( 30 .5f);
floatBuffer.put( 40 .5f);
floatBuffer.mark();
System.out.println(
"FloatBuffer before "
+ "setting buffer's limit: "
+ Arrays.toString(
floatBuffer.array())
+ "\nPosition: "
+ floatBuffer.position()
+ "\nLimit: "
+ floatBuffer.limit());
floatBuffer.limit( 4 );
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-
Share your thoughts in the comments
Please Login to comment...