# DoubleBuffer wrap() method in Java

Java 8Object Oriented ProgrammingProgramming

A double array can be wrapped into a buffer using the method wrap() in the class java.nio.DoubleBuffer. This method requires a single parameter i.e. the array to be wrapped into a buffer and it returns the new buffer created. If the returned buffer is modified, then the contents of the array are also similarly modified and vice versa.

A program that demonstrates this is given as follows −

## Example

Live Demo

import java.nio.*;
import java.util.*;
public class Demo {
public static void main(String[] args) {
try {
double[] arr = { 8.7D, 1.5D, 3.2D, 7.4D, 5.9D };
System.out.println("The array length is: " + arr.length);
System.out.println("Array elements are: " + Arrays.toString(arr));
DoubleBuffer buffer = DoubleBuffer.wrap(arr);
buffer.rewind();
System.out.println("\nThe DoubleBuffer is: " + Arrays.toString(buffer.array()));
System.out.println("The position is: " + buffer.position());
System.out.println("The capacity is: " + buffer.capacity());
} catch (IllegalArgumentException e) {
System.out.println("Error!!! IllegalArgumentException");
}
}
}

The output of the above program is as follows −

## Output

The array length is: 5
Array elements are: [8.7, 1.5, 3.2, 7.4, 5.9]
The DoubleBuffer is: [8.7, 1.5, 3.2, 7.4, 5.9]
The position is: 0
The capacity is: 5
Published on 01-Mar-2019 06:15:22