IntBuffer wrap() method in Java
wrap(int[] array)
The wrap() method of java.nio.IntBuffer Class is used to wrap a int array into a buffer. The new buffer will be backed by the given int array; that is, modifications to the buffer will cause the array to be modified and vice versa. The new buffer’s capacity and limit will be array.length, its position will be zero, and its mark will be undefined. Its backing array will be the given array, and its array offset will be zero.
Syntax:
public static IntBuffer wrap(int[] array)
Parameters: This method takes array which is the array that will back this buffer, as a parameter.
Return Value: This method returns the the new int buffer created.
Below are the examples to illustrate the wrap() method:
Examples 1:
// Java program to demonstrate // wrap() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declare and initialize the int array int[] ibb = { 1, 2, 3 }; // print the int array length System.out.println("Array length : " + ibb.length); // print the int array element System.out.println("\nArray element : " + Arrays.toString(ibb)); // wrap the int array into intBuffer // using wrap() method IntBuffer intBuffer = IntBuffer.wrap(ibb); // Rewind the intbuffer intBuffer.rewind(); // print the int buffer System.out.println("\nintBuffer : " + Arrays.toString(intBuffer.array())); // print the IntBuffer capacity System.out.println("\nintbuffer capacity : " + intBuffer.capacity()); // print the IntBuffer position System.out.println("\nintbuffer position: " + intBuffer.position()); } } |
Array length : 3 Array element : [1, 2, 3] intBuffer : [1, 2, 3] intbuffer capacity : 3 intbuffer position: 0
wrap(int[] array, int offset, int length)
The wrap() method wraps an int array into a buffer. The new buffer will be backed by the given int array; that is, modifications to the buffer will cause the array to be modified and vice versa. The new buffer’s capacity will be array.length, its position will be offset, its limit will be offset + length, and its mark will be undefined. Its backing array will be the given array, and its array offset will be zero.
Syntax:
public static IntBuffer
wrap (int[] array, int offset, int length)
Parameters: This method takes following parameters:
- array: The array that will back the new buffer.
- offset: The offset of the subarray to be used; must be non-negative and no larger than array.length. The new buffer’s position will be set to this value.
- length: The length of the subarray to be used; must be non-negative and no larger than array.length – offset. The new buffer’s limit will be set to offset + length.
Return Value: This method returns the new float buffer.
Throws: This method throws the IndexOutOfBoundsException if the preconditions on the offset and length parameters do not hold.
Below are the examples to illustrate the wrap() method:
Examples 1:
// Java program to demonstrate // wrap() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declare and initialize the int array int[] ibb = { 1, 2, 3 }; // print the int array length System.out.println("Array length : " + ibb.length); // print the int array element System.out.println("\nArray element : " + Arrays.toString(ibb)); // wrap the int array into intBuffer // using wrap() method IntBuffer intBuffer = IntBuffer.wrap(ibb, 0, ibb.length); // Rewind the intbuffer intBuffer.rewind(); // print the int buffer System.out.println("\nintBuffer : " + Arrays.toString(intBuffer.array())); // print the IntBuffer capacity System.out.println("\nintbuffer capacity : " + intBuffer.capacity()); // print the IntBuffer position System.out.println("\nintbuffer position: " + intBuffer.position()); } } |
Array length : 3 Array element : [1, 2, 3] intBuffer : [1, 2, 3] intbuffer capacity : 3 intbuffer position: 0
Examples 2: To demonstrate NullPointerException
// Java program to demonstrate // wrap() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declare and initialize the float array int[] ibb = { 1, 2, 3 }; // print the int array length System.out.println("Array length : " + ibb.length); // print the int array element System.out.println("\nArray element : " + Arrays.toString(ibb)); try { // wrap the int array into intBuffer // using wrap() method System.out.println("\nHere " + "offset and length does not hold" + " the required condition "); IntBuffer intBuffer = IntBuffer.wrap(ibb, 1, ibb.length); // Rewind the intbuffer intBuffer.rewind(); // print the int buffer System.out.println("\nintBuffer : " + Arrays.toString(intBuffer.array())); // print the IntBuffer capacity System.out.println("\nintbuffer capacity : " + intBuffer.capacity()); // print the IntBuffer position System.out.println("\nintbuffer position: " + intBuffer.position()); } catch (IndexOutOfBoundsException e) { System.out.println("Exception throws: " + e); } } } |
Array length : 3 Array element : [1, 2, 3] Here offset and length does not hold the required condition Exception throws: java.lang.IndexOutOfBoundsException
Recommended Posts:
- IntBuffer hasArray() method in Java
- IntBuffer compareTo() method in Java
- IntBuffer array() method in Java
- IntBuffer compact() method in Java
- IntBuffer slice() method in Java
- IntBuffer allocate() method in Java
- IntBuffer equals() method in Java
- IntBuffer arrayOffset() method in Java
- IntBuffer asReadOnlyBuffer() method in Java
- IntBuffer rewind() method in Java with Examples
- IntBuffer flip() method in Java with Examples
- IntBuffer clear() method in Java with Examples
- IntBuffer mark() method in Java with Examples
- IntBuffer limit() method in Java with Examples
- IntBuffer duplicate() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : ManasChhabra2



