Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

What is the IntBuffer clear() method in Java?

Samia Ishaque

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.

What is IntBuffer?

InfBuffer is a class in Java that is defined inside the java.nio package. The java.nio package defines buffers, which are a chunk of readable and writeable memory that can be used to easily communicate over NIO channels. There are many types of buffers in the java.nio package, including IntBuffer. It is similar to arrays of different data types with different sizes.

The class IntBuffer allows for various operations, which are as follows:

  • absolute and relative get methods that read single ints.
  • absolute and relative put methods that write single ints.
  • relative bulk get method that can allocate a contiguous chunk of memory from the buffer into arrays.
  • relative bulk put method that can allocate contiguous memory from an int array to a buffer.

Overview

The clear method in the IntBuffer class is used to clear up a buffer. This means values such as limit and mark are reset to that of an initial buffer. The position is also set to zero.

The prototype for the clear method is as follows:

public final IntBuffer clear()

Parameters

There are no mandatory parameters for the clear method.

Return value

The clear method returns the buffer with the position set to 0, mark discard, and limit set to capacity.

Example

The following example will help you understand the clear method better:

import java.nio.*;
import java.util.*;
public class main {
public static void main(String[] args)
{
int cap = 4;
IntBuffer buffer = IntBuffer.allocate(cap);
buffer.put(0,1);
buffer.put(2);
buffer.put(3);
buffer.put(3);
System.out.println("Printing the buffer position before clearing: "
+ buffer.position());
System.out.println("Printing the buffer limit before clearing: "
+ buffer.limit());
buffer.clear();
System.out.println("Printing the buffer position after clearing: "
+ buffer.position());
System.out.println("Printing the buffer limit after clearing: "
+ buffer.limit());
}
}

First, we create a buffer and fill it up with integers. Then, we clear it using the clear method. As shown, the values for position changes from 3 to 0.

RELATED TAGS

java

CONTRIBUTOR

Samia Ishaque
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