Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

What is the ShortBuffer clear() method in Java?

Syed Muhammad Ali Mustafa

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

java.nio.ShortBuffer is a class we can use to store a buffer of shorts. We can use this class’s clear() method to clear a buffer. Clearing a buffer means:

  • Setting the buffer position equal to 0.
  • Setting the buffer limit equal to the buffer capacity.

Declaration

The ShortBuffer.clear() method is declared as follows:

buff.clear()
  • buff: The ShortBuffer to be cleared.

Return value

The ShortBuffer.clear() method returns the ShortBuffer buff after clearing it.

Code

Consider the code snippet below, which demonstrates the use of the ShortBuffer.clear() method.

import java.nio.*;
import java.util.*;
public class main {
public static void main(String[] args) {
int n1 = 4;
ShortBuffer buff1 = ShortBuffer.allocate(n1);
buff1.put((short)1);
buff1.put((short)4);
buff1.limit(3);
buff1.mark();
System.out.println("buff1: " + Arrays.toString(buff1.array()));
System.out.println("position at(before clear): " + buff1.position());
System.out.println("Limit at(before clear): " + buff1.limit());
buff1.mark();
System.out.println("clear()");
buff1.clear();
System.out.println("position at(after rewind): " + buff1.position());
System.out.println("Limit at(before clear): " + buff1.limit());
}
}

Explanation

  • A ShortBuffer buff1 is declared in line 7 with the capacity n1 = 4.
  • Two elements are added to buff1 using the put() method in lines 8-9. After adding the first element, the position of buff1 is incremented from 0 to 1. After adding the second element, the position of buff1 is incremented from 1 to 2.
  • The position of buff1 before clearing is 2. After calling the clear() method in line 21, the position of buff1 is set to 0.
  • The limit of buff1 is set to 3 using the limit() method in line 11. The limit of buff1 before clearing it is 3. After calling the clear() method in line 21, the limit of buff1 is set to its capacity, which is 4.
  • The position of buff1 is marked to 2 using the mark() method in line 12. After calling the clear() method in line 21, the mark of buff1 is discarded and the position is set to 0.

RELATED TAGS

java

CONTRIBUTOR

Syed Muhammad Ali Mustafa
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring