DoubleBuffer limit() methods in Java with Examples
The limit() method of java.nio.DoubleBuffer Class is used to modify this DoubleBuffer’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 DoubleBuffer limit(int newLimit)
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:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
DoubleBuffer doubleBuffer
= DoubleBuffer.allocate( 4 );
doubleBuffer.put( 20.5 );
doubleBuffer.put( 30.5 );
System.out.println( "DoubleBuffer before "
+ "setting buffer's limit: "
+ Arrays.toString(
doubleBuffer.array())
+ "\nPosition: "
+ doubleBuffer.position()
+ "\nLimit: "
+ doubleBuffer.limit());
doubleBuffer.limit( 1 );
System.out.println( "\nDoubleBuffer after "
+ "setting buffer's limit: "
+ Arrays.toString(
doubleBuffer.array())
+ "\nPosition: "
+ doubleBuffer.position()
+ "\nLimit: "
+ doubleBuffer.limit());
}
}
|
Output:
DoubleBuffer before setting buffer's limit: [20.5, 30.5, 0.0, 0.0]
Position: 2
Limit: 4
DoubleBuffer 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)
{
DoubleBuffer doubleBuffer
= DoubleBuffer.allocate( 5 );
doubleBuffer.put( 20.5 );
doubleBuffer.put( 30.5 );
doubleBuffer.put( 40.5 );
doubleBuffer.mark();
System.out.println( "DoubleBuffer before "
+ "setting buffer's limit: "
+ Arrays.toString(
doubleBuffer.array())
+ "\nPosition: "
+ doubleBuffer.position()
+ "\nLimit: "
+ doubleBuffer.limit());
doubleBuffer.limit( 4 );
System.out.println( "\nDoubleBuffer before "
+ "setting buffer's limit: "
+ Arrays.toString(
doubleBuffer.array())
+ "\nPosition: "
+ doubleBuffer.position()
+ "\nLimit: "
+ doubleBuffer.limit());
}
}
|
Output:
DoubleBuffer before setting buffer's limit: [20.5, 30.5, 40.5, 0.0, 0.0]
Position: 3
Limit: 5
DoubleBuffer 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/DoubleBuffer.html#limit-int-
Last Updated :
29 Jul, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...