byte等基础类型 认识

222 阅读4分钟

1.1. bit

bit是计算机最小字节,存放0/1。1byte = 8bit,所以byte得范围是-128~127,char2个字节,int4个字节,double8个字节

1.2. buffer

buffer(A container for data of a specific primitive type.一个特殊原始类型的容器为了保存数据)。( A buffer is a linear, finite sequence of elements of a specific primitive type. Aside from its content, the essential properties of a buffer are its capacity, limit, and position buffer是一个特定基本元素类型的线性,有限的序列。除了他的内容,buffer的基本属性还有capacity容量,limit限制大小,position位置) buffer的所有方法都比较简答,主要就是自身属性信息的设置和返回。

2.1基本属性

  • capacity:

A buffer's capacity is the number of elements it contains. The capacity of a buffer is never negative and never changes. buffer的cap是元素的数量。cap不可能是负数和改变。

  • limit

A buffer's limit is the index of the first element that should not be read or written. A buffer's limit is never negative and is never greater than its capacity.

很多情况下,buffer不是被填满的。limit是第一个不该被读写的元素的索引位置,buffer 的limit不是负数或者大于cap
  • position

A buffer's position is the index of the next element to be read or written. A buffer's position is never negative and is never greater than its limit.

position是下一个要被读写的元素位置。不是负数或者大于limit
  • relative 相对位置

从当前position读取一个或者多个元素。不断移动增加position位置。如果get请求超过了limit位置,会抛出BufferUnderflowException。如果put操作超过了limit,会抛出BufferOverflowException。上面两种情况,数据都不会传输。

  • absolute

绝对位置操作,显示提供index,不会影响position的位置。如果索引超过limit,会抛出IndexOutofBoundsException。

  • mark

被标记的索引,。调用reset方法会将position的值设为mark的值。这样就能重新读写buffer的数据。当position或者limit小于mark,mark的值会被丢弃。如果没有设置mark,调用reset会抛出InvalidMarkException。

0 <= mark <= position <= limit <= capacity
> 一个新创建的buffer的position是0,mark未定义。初始的limit可能为0或其他值。初始化的uffer包含0个元素。
  • Clearing

清空。让buffer可以接受新的数据读取。清空缓冲区,position设置为0,limit设置为cap,mark被丢弃。

public final Buffer clear() {
        position = 0;
        limit = capacity;
        mark = -1;
        return this;
    }
  • flipping

翻转。让buffer可以接受新的数据填充。反转缓冲区会将limit=position,然后position设置为0.mark呗丢弃。

public final Buffer flip() {
        limit = position;
        position = 0;
        mark = -1;
        return this;
    }
  • rewinding

回退。重新读取buffer的值。position设置为0,mark被丢弃,limit不变。

public final Buffer rewind() {
        position = 0;
        mark = -1;
        return this;
    }
  • reset
public final Buffer reset() {
        int m = mark;
        if (m < 0)
            throw new InvalidMarkException();
        position = m;
        return this;
    }

2.2 其他特性

可以设置isReadOnly 只读buffer。 非线程安全

一些初始化值
    // Invariants: mark <= position <= limit <= capacity
    private int mark = -1;
    private int position = 0;
    private int limit;
    private int capacity;

    // Used only by direct buffers
    // NOTE: hoisted here for speed in JNI GetDirectBufferAddress
    long address;

1.3. ByteBuffer

ByteBuffer是Buffer的一个子类。

  1. direct 和 no-direct buffer

direct buffer the Java virtual machine will make a best effort to perform native I/O operations directly upon it。通过allocation来分配。也就是说,它会尝试避免将缓冲区的内容复制到中间缓冲区去,在每次调用其中一个操作之前或者之后都是在系统navite内存。避免内存拷贝。分配比较耗时间,而且分配的空间有可能在GC区域之外。推荐使用大块且经常使用的内存。

  1. view

可以为同一种类型的数据构建视图。每个视图都是独立的。但是基础buffer的变动会体现在视图上面。如果当前buffer是direct,创建出的view也是direct。

// These fields are declared here rather than in Heap-X-Buffer in order to
// reduce the number of virtual method invocations needed to access these
// values, which is especially costly when coding small buffers.
final byte[] hb;   // Non-null only for heap buffers
final int offset;
boolean isReadOnly;  // Valid only for heap buffers
  1. DirectByteBuffer
// Primary constructor
    //
    DirectByteBuffer(int cap) {                   // package-private

        super(-1, 0, cap, cap);
        boolean pa = VM.isDirectMemoryPageAligned();
        int ps = Bits.pageSize();
        long size = Math.max(1L, (long)cap + (pa ? ps : 0));
        //预分配内存空间
        Bits.reserveMemory(size, cap);

        long base = 0;
        try {
            base = unsafe.allocateMemory(size);
        } catch (OutOfMemoryError x) {
            Bits.unreserveMemory(size, cap);
            throw x;
        }
        unsafe.setMemory(base, size, (byte) 0);
        if (pa && (base % ps != 0)) {
            // Round up to page boundary
            address = base + ps - (base & (ps - 1));
        } else {
            address = base;
        }
        cleaner = Cleaner.create(this, new Deallocator(base, size, cap));
        att = null;



    }

5.allocate(int capacity)

分配的是堆内存

1.4 char

只能存放单个字符,对应ascii里面的字符 类型转换顺序:byte->short(char)->int->long->float->double 数据类型从右边转换成左边的类型时,必须要进行强制转换,且转换时会丢失部分信息。 byte是字节数据类型,是有符号型的,char是字符数据类型,无符号,占连个字节