Skip to content
Related Articles

Related Articles

ShortBuffer clear() methods in Java with Examples
  • Last Updated : 26 Aug, 2019

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

  • the current position is set to zero
  • the limit of this ShortBuffer instance is set to the capacity
  • the mark, if any, of this ShortBuffer instance is discarded.

Syntax:

public final ShortBuffer clear()

Return Value: This method returns this ShortBuffer 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 {
  
            short[] darr = { 2, 3, 4, 6 };
  
            // creating object of ShortBuffer
            // and allocating size capacity
            ShortBuffer db
                = ShortBuffer.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)
    {
  
        short[] carr = { 2, 10, 13, 23 };
  
        // creating object of ShortBuffer
        // and allocating size capacity
        ShortBuffer db = ShortBuffer.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/ShortBuffer.html#clear–

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :