Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

What is the IntBuffer duplicate() 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.

Answers Code

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. 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 duplicate method is used to copy one buffer of type integer to another. Its prototype in the IntBuffer class is as follows:

public abstract IntBuffer duplicate()

Parameters

The duplicate method takes no mandatory parameters as input.

Return value

The duplicate method returns the new buffer of type integer that carries the copied contents.

Example

The following example will help you understand the duplicate method better. As shown below, we create an IntBuffer using the method allocate and fill it with elements using the method put. Then, making use of the duplicate method, we copy the contents of the old buffer into a duplicated one.

import java.nio.*;
import java.util.*;
public class main {
public static void main(String[] args)
{
int cap = 3;
IntBuffer buffer = IntBuffer.allocate(cap);
buffer.put(1);
buffer.put(2);
buffer.put(3);
System.out.println("Old Buffer: "
+ Arrays.toString(buffer.array()));
IntBuffer dup = buffer.duplicate();
System.out.println("\nDuplicated Buffer: "
+ Arrays.toString(dup.array()));
}
}

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.

Answers Code
Keep Exploring