LongBuffer clear() method in Java with Examples Last Updated : 26 Aug, 2019 Comments Improve Suggest changes Like Article Like Report The clear() method of java.nio.LongBuffer 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 LongBuffer clear() Parameter: The method do not take any parameter. Return Value: This method returns this LongBuffer instance after clearing all the data from it. Below are the examples to illustrate the clear() method: Examples 1: Java // Java program to demonstrate // clear() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { try { long[] arr = { 10, 20, 30, 40 }; // creating object of LongBuffer // and allocating size capacity LongBuffer lb = LongBuffer.wrap(arr); // try to set the position at index 2 lb.position(2); // Set this buffer mark position // using mark() method lb.mark(); // try to set the position at index 4 lb.position(4); // display position System.out.println("position before reset: " + lb.position()); // try to call clear() to restore // to the position at index 0 // by discarding the mark lb.clear(); // display position System.out.println("position after reset: " + lb.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 // Java program to demonstrate // clear() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { long[] arr = { 10, 20, 30, 40 }; // creating object of LongBuffer // and allocating size capacity LongBuffer lb = LongBuffer.wrap(arr); // try to set the position at index 3 lb.position(3); // display position System.out.println("position before clear: " + lb.position()); // try to call clear() to restore // to the position at index 0 // by discarding the mark lb.clear(); // display position System.out.println("position after clear: " + lb.position()); } } Output: position before clear: 3 position after clear: 0 Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/LongBuffer.html Comment More infoAdvertise with us Next Article LongBuffer clear() method in Java with Examples rohitprasad3 Follow Improve Article Tags : Java Java-Functions Java-NIO package Java-LongBuffer Practice Tags : Java Similar Reads LongBuffer mark() method in Java with Examples The mark() method of java.nio.LongBuffer Class is used to mark the current position of this LongBuffer as the mark of this buffer. Syntax: public LongBuffer mark() Parameter: This method do not accept any parameter. Return Value: This method returns this LongBuffer after setting the buffer's mark at 2 min read LongBuffer flip() method in Java with Examples The flip() method of java.nio.LongBuffer Class is used to flip this buffer. By flipping this buffer, it meant that the buffer will be trimmed to the current position the then the position will be changed to zero if any mark is there on the buffer, then that mark will be automatically discarded Synta 2 min read LongBuffer reset() method in Java with Examples The reset() method of java.nio.LongBuffer Class is used to reset this buffer's position to the previously-marked position. The mark's value remain unchanged during this process. Syntax: public final LongBuffer reset() Parameter: This method do not accept any parameter. Return Value: This method retu 2 min read LongBuffer rewind() method in Java with Examples The rewind() method of java.nio.LongBuffer Class is used to rewind this buffer. By rewinding this Buffer, the following actions are taken: Current position is set to zero the mark is discarded, if any, but the mark value is unchanged. Syntax: public LongBuffer rewind() Parameter: This method do not 2 min read List clear() method in Java with Examples The clear() method of List interface in Java is used to remove all of the elements from the List container. This method does not deleted the List container, instead it just removes all of the elements from the List. Example:Java// Java Program to Demonstrate // List clear() import java.util.*; class 2 min read Map clear() method in Java with Example The java.util.Map.clear() method in Java is used to clear and remove all of the elements or mappings from a specified Map collection. Syntax: void clear() Parameters: The method does not accept any parameters. Return Value: The method does not return any value. Below programs are used to illustrate 2 min read Set clear() method in Java with Examples The Java.util.Set.clear() method is used to remove all the elements from a Set. Using the clear() method only clears all the element from the set and not deletes the set. In other words, we can say that the clear() method is used to only empty an existing Set. Syntax: void clear() Parameters: The me 1 min read LinkedHashSet clear() method in Java with Examples The clear() method of java.util.LinkedHashSet class is used to remove all of the elements from this set. The set will be empty after this call returns. Syntax: public void clear() Return Value: This method does not return anything. Below are the examples to illustrate the clear() method. Example 1: 2 min read Locale.Builder clear() method in Java with Examples The clear() method of java.util.Locale.Builder class in Java is used to reset this Locale.Builder. It means that this method will create another instance of Locale.Builder with all the properties reset to their initial state and return it. Syntax: public Locale.Builder clear() Parameter: This method 1 min read Stack clear() method in Java with Example The Java.util.Stack.clear() method is used to remove all the elements from a Stack. Using the clear() method only clears all the element from the Stack and does not delete the Stack. In other words, we can say that the clear() method is used to only empty an existing Stack. Syntax: Stack.clear() Par 2 min read Like