Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

What is the duplicate() method of the DoubleBuffer class in Java?

Talha Ashar

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.

The duplicate() method of the DoubleBuffer class in Java creates a new DoubleBuffer object that shares the content of another buffer.

The process is illustrated below:

The values in the source DoubleBuffer are all copied into the newly created buffer. Any changes made to the original buffer are reflected in the new buffer and vice versa.

The properties of the new buffer, e.g., position, limit, and capacity, are identical to the source buffer; however, these properties are also independent.

Similarly, properties such as whether the new buffer is direct or read-only depend on the properties of the source buffer.

You can check the documentation for a comprehensive guide on the properties of a DoubleBuffer.

To use the duplicate() method, you will need to import the DoubleBuffer class into your program, as shown below:

import java.nio.DoubleBuffer

Parameters

The duplicate() method does not accept any parameters.

Return value

The duplicate() method returns the newly created DoubleBuffer object.

Example

The code below shows how the duplicate() method can be used in Java:

import java.nio.*;
import java.util.*;
class duplicateMethod {
public static void main(String[] args) {
// initialize buffer instance
DoubleBuffer sourceBuffer = DoubleBuffer.allocate(5);
// add values to buffer
sourceBuffer.put(0.5D);
sourceBuffer.put(2.2D);
sourceBuffer.put(6.4D);
// create a duplicate buffer
DoubleBuffer newBuffer = sourceBuffer.duplicate();
// Print buffers
System.out.println("The source buffer is: " + Arrays.toString(sourceBuffer.array()));
System.out.println("The duplicate buffer is: " + Arrays.toString(newBuffer.array()));
// add values to source buffer
sourceBuffer.put(4.1D);
sourceBuffer.put(9.9D);
// Print buffers
System.out.println("\nThe updated source buffer is: " + Arrays.toString(sourceBuffer.array()));
System.out.println("The updated duplicate buffer is: " + Arrays.toString(newBuffer.array()));
}
}

Explanation

First, a DoubleBuffer object called sourceBuffer is initialized through the allocate() method. sourceBuffer has the capacity to store 55 values.

Next, the put() method is invoked thrice to add values to sourceBuffer.

The duplicate() method is invoked in line 1616 to create a new DoubleBuffer object called newBuffer. All the content of sourceBuffer is shared with newBuffer.

Finally, the put() method is invoked to add 22 more values to sourceBuffer. Since content is shared between both DoubleBuffer objects, the changes are also reflected in newBuffer, and both objects are identical when printed.

RELATED TAGS

java

CONTRIBUTOR

Talha Ashar
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