What is the IntBuffer reset() method in Java?

What is IntBuffer?

IntBuffer 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. IntBuffer 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 reset() method in Java resets the position to the one previously marked using the mark method. The position in an IntBuffer refers to the index being pointed to.

Parameter

There is no mandatory parameter for the reset() method.

Return

The reset() method returns the buffer.

Example

The following example will help you understand the reset() method better. First, we create an array of type int and use the wrap method to create an IntBuffer. Then, we set the position of the index using the position method and mark it using the mark method. We change the position of the index and print the current position. Lastly, we use the reset method to reset the current position of the index to the one we marked and print it.

import java.nio.*;
import java.util.*;
public class EX{
public static void main(String[] args)
{
int[] intarray = {1,2,3,4};
IntBuffer buff = IntBuffer.wrap(intarray);
buff.position(1);
buff.mark();
buff.position(3);
System.out.println("Position before using reset() method: "
+ buff.position());
buff.reset();
System.out.println("Position after using reset() method: "
+ buff.position());
}
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved