1:概述
Java提供了七个基本的缓冲区,分别由七个类来管理,它们都可以在java.nio包中找到。这七个类如下所示:
ByteBuffer
ShortBuffer
IntBuffer
CharBuffer
FloatBuffer
DoubleBuffer
LongBuffer
st1":*{behavior:url(#ieooui) }
1. 通过静态方法allocate来创建缓冲区。
这七类都有一个静态的allocate方法,通过这个方法可以创建有最大容量限制的缓冲区对象。allocate的定义如下:
ByteBuffer类中的allocate方法:
public static ByteBuffer allocate(int capacity)
IntBuffer类中的allocate方法:
public static IntBuffer allocate(int capacity)
其他五个缓冲区类中的allocate 方法定义和上面的定义类似,只是返回值的类型是相应的缓冲区类。
allocate方法有一个参数capacity,用来指定缓冲区容量的最大值 . capacity的不能小于0,否则会抛出一个IllegalArgumentException异常 , 1024*1024(1M)
2:通过静态方法allocate来创建缓冲区
allocate的使用方法如下:
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
IntBuffer intBuffer = IntBuffer.allocate(