Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

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

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 easily communicate over NIO channels. There are many buffers in the java.nio package, including IntBuffer. It is similar to arrays of different data types with different sizes.

The IntBuffer class 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 methods that can allocate a contiguous chunk of memory from the buffer into arrays.
  • relative bulk put methods that can allocate contiguous memory from an int array to a buffer.

Overview

The wrap method in the IntBuffer class wraps an array into a buffer of type integer. Any changes to the buffer will be reflected in the array and vice versa. Moreover, the index is set to 0, and the mark is undefined. The wrap method’s prototype is as follows:

public static IntBuffer wrap(int[] array)

Parameters

The wrap method takes an array of type integer as a mandatory parameter.

Return value

The wrap method returns a buffer of type integer.

Example

The following example will help you understand how to use the wrap method. First, we create an array of data type integer and print its size and elements. Then, we use the wrap method to wrap the integer array into a buffer. After using the wrap method, we print the buffer. Then, we print the buffer’s capacity and position using the methods capacity and position, respectively. We do this to compare our buffer’s print with the array and affirm that they are similar. Moreover, we can also see the capacity of the array reflected as the buffer’s capacity and the initial index set to 0 below:

import java.nio.*;
import java.util.*;
public class EX{
public static void main(String[] args)
{
int[] intarray = {1,2,3,4};
System.out.println("The length of array : "
+ intarray.length);
System.out.println("\nPrinting of the array : "
+ Arrays.toString(intarray));
IntBuffer buff = IntBuffer.wrap(intarray);
System.out.println("\nPrinting of the IntBuffer : "
+ Arrays.toString(buff.array()));
System.out.println("\nBuffer's capacity : "
+ buff.capacity());
System.out.println("\nBuffer's position: "
+ buff.position());
}
}

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.

Keep Exploring