FloatBuffer mark() methods in Java with Examples
The mark() method of java.nio.FloatBuffer Class is used to mark the current position of this FloatBuffer as the mark of this buffer.
Syntax:
public final FloatBuffer mark()
Parameters: The method does not take any parameters.
Return Value: This method returns this FloatBuffer after setting the buffer’s mark at the current position.
Below examples illustrate the mark() method:
Examples 1:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
try {
float [] farr = { 10 .5f, 20 .5f, 30 .5f, 40 .5f };
FloatBuffer fb = FloatBuffer.wrap(farr);
fb.position( 2 );
fb.mark();
fb.position( 4 );
System.out.println( "position before reset: "
+ fb.position());
fb.reset();
System.out.println( "position after reset: "
+ fb.position());
}
catch (InvalidMarkException e) {
System.out.println( "New position "
+ "is less than "
+ "the position "
+ "marked before " );
System.out.println( "Exception throws: "
+ e);
}
}
}
|
Output:
position before reset: 4
position after reset: 2
Examples 2: To demonstrate InvalidMarkException
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
try {
float [] farr = { 10 .5f, 20 .5f, 30 .5f, 40 .5f };
FloatBuffer fb = FloatBuffer.wrap(farr);
fb.position( 2 );
fb.mark();
fb.position( 1 );
System.out.println( "position before reset: "
+ fb.position());
fb.reset();
System.out.println( "position after reset: "
+ fb.position());
}
catch (InvalidMarkException e) {
System.out.println( "\nNew position "
+ "is less than "
+ "the position "
+ "marked before " );
System.out.println( "Exception throws: "
+ e);
}
}
}
|
Output:
position before reset: 1
New position is less than the position marked before
Exception throws: java.nio.InvalidMarkException
Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/FloatBuffer.html#mark–
Last Updated :
10 Sep, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...