Open In App

DoubleBuffer clear() methods in Java with Examples

Last Updated : 29 Jul, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The clear() method of java.nio.CharBuffer Class is used to clear this buffer. While clearing this buffer following changes are done:

  • the position is set to zero
  • the limit is set to the capacity
  • the mark is discarded.

Syntax:

public final DoubleBuffer clear()

Return Value: This method returns this DoubleBuffer instance after clearing all the data from it.

Below are the examples to illustrate the clear() method:

Examples 1:




// Java program to demonstrate
// clear() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        try {
  
            double[] darr = { 2.5, 3.5, 4.5, 6.7 };
  
            // creating object of DoubleBuffer
            // and allocating size capacity
            DoubleBuffer db
                = DoubleBuffer.wrap(darr);
  
            // try to set the position at index 2
            db.position(2);
  
            // Set this buffer mark position
            // using mark() method
            db.mark();
  
            // try to set the position at index 4
            db.position(4);
  
            // display position
            System.out.println("position before reset: "
                               + db.position());
  
            // try to call clear() to restore
            // to the position at index 0
            // by discarding the mark
            db.clear();
  
            // display position
            System.out.println("position after reset: "
                               + db.position());
        }
  
        catch (InvalidMarkException e) {
            System.out.println("new position is less than "
                               + "the position we "
                               + "marked before ");
            System.out.println("Exception throws: " + e);
        }
    }
}


Output:

position before reset: 4
position after reset: 0

Examples 2:




// Java program to demonstrate
// clear() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        double[] carr = { 2.4, 105.4, 13.9, 23.45d };
  
        // creating object of DoubleBuffer
        // and allocating size capacity
        DoubleBuffer db = DoubleBuffer.wrap(carr);
  
        // try to set the position at index 3
        db.position(3);
  
        // display position
        System.out.println("position before clear: "
                           + db.position());
  
        // try to call clear() to restore
        // to the position at index 0
        // by discarding the mark
        db.clear();
  
        // display position
        System.out.println("position after clear: "
                           + db.position());
    }
}


Output:

position before clear: 3
position after clear: 0

Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/DoubleBuffer.html#clear–



Similar Reads

DoubleBuffer put() methods in Java with Examples | Set 1
put(double f) The put(double f) method of java.nio.DoubleBuffer Class is used to write the given double into the newly created double buffer at the current position, and then increments the position. Syntax: public abstract DoubleBuffer put(double f) Parameters: This method takes the double value f as a parameter which is to be written in double bu
6 min read
DoubleBuffer get() methods in Java with Examples
The get() method of java.nio.DoubleBuffer Class is used to reads the double at the given buffer’s current position, and then increments the position. Syntax: public abstract double get() Return Value: This method returns the double value at the buffer’s current position. Exception: This method throws BufferUnderflowException if the buffer’s current
3 min read
DoubleBuffer flip() methods in Java with Examples
The flip() method of java.nio.DoubleBuffer Class is used to flip this buffer. By flipping this buffer, it meant that the buffer will be trimmed to the current position and then the position will be changed to zero. During this process, if any mark is there on the buffer, then that mark will be automatically discarded. Syntax: public final DoubleBuf
2 min read
DoubleBuffer mark() methods in Java with Examples
The mark() method of java.nio.DoubleBuffer Class is used to mark the current position of this DoubleBuffer as the mark of this buffer. Syntax: public DoubleBuffer mark() Return Value: This method returns this DoubleBuffer after setting the buffer's mark at the current position. Below are the examples to illustrate the mark() method: Examples 1: //
2 min read
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 fi
2 min read
DoubleBuffer order() methods in Java with Examples
The order() method of java.nio.DoubleBuffer class is used to get the ByteOrder of this DoubleBuffer instance. Syntax: public abstract ByteOrder order() Return Value: This method returns this buffer's byte order.Below are the examples to illustrate the order() method:Examples 1: Java Code // Java program to demonstrate // order() method import java.
2 min read
DoubleBuffer reset() methods in Java with Examples
The reset() method of java.nio.DoubleBuffer Class is used to reset the position of this buffer to a position which was previously-marked. In this, neither a marked value is changed nor discarded. It helps to recover the previously marked value in the buffer. Syntax: public final DoubleBuffer reset() Parameters: The method does not take any paramete
2 min read
DoubleBuffer arrayOffset() method in Java With Examples
The arrayOffset() method of java.nio.DoubleBuffer class is used to return the offset within the buffer’s backing array of the first element of the buffer. It means that if this buffer is backed by an array, then buffer position p corresponds to array index p + arrayOffset(). Inorder to check whether this buffer has a backing array, hasArray() metho
3 min read
DoubleBuffer compareTo() method in Java With Examples
The compareTo() method of java.nio.DoubleBuffer class is used to compare one buffer to another. Two double buffers are compared by comparing their sequences of remaining elements lexicographically, without regard to the starting position of each sequence within its corresponding buffer. Pairs of float elements are compared as if by invoking Double.
3 min read
DoubleBuffer wrap() method in Java with Examples
wrap(double[] array) The wrap() method of java.nio.DoubleBuffer Class is used to wraps a double array into a buffer. The new buffer will be backed by the given double array; that is, modifications to the buffer will cause the array to be modified and vice versa. The new buffer’s capacity and limit will be array.length, its position will be zero, an
4 min read
Practice Tags :