Share on Google+Share on Google+

Use of getInt() method of ByteBuffer class in java.

In this tutorial you will see the use of getInt() method of ByteBuffer class in java.

Use of getInt() method of ByteBuffer class in java.

 In this tutorial, we will see how to read integer value from byte buffer by using getInt() method of ByteBuffer class.

ByteBuffer API:

The java.nio.ByteBuffer class extends java.nio.Buffer class. It provides the following methods:

Return type Method Description
static ByteBuffer allocate(int capacity)  The allocate(..)method allocate a new byte buffer.
abstract ByteBuffer putInt(int index, byte b) The putInt(..) method write four byte containing the given int value into associated buffer.
abstract byte getInt() The getInt() method read 4 byte from current position and increment position.


import java.nio.*;
import java.nio.ByteBuffer;
public class GetIntBuffer {
  public static final int capacity = 100;
public static void main(String[] argvthrows Exception{
    ByteBuffer bytebuf = ByteBuffer.allocate(capacity);
System.out.println("Integer data into byte buffer : "
        + bytebuf.getInt());


C:\>java GetIntBuffer
Integer data into byte buffer : 221255

Download this code


Posted on: July 28, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null