猿问

Vector源码疑惑

1.在看Vector源码是有2个小知识点比较疑惑
2.疑惑代码如下
/**


    • This implements the unsynchronized semantics of ensureCapacity.

    • Synchronized methods in this class can internally call this

    • method for ensuring capacity without incurring the cost of an

    • extra synchronization.
      *

    • @see #ensureCapacity(int)
      */

    1. void ensureCapacityHelper(int minCapacity) {

         // overflow-conscious code
         if (minCapacity - elementData.length > 0)
             grow(minCapacity);

      }

      /**

      • The maximum size of array to allocate.

      • Some VMs reserve some header words in an array.

      • Attempts to allocate larger arrays may result in

      • OutOfMemoryError: Requested array size exceeds VM limit
        */

    2. static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
      3.疑惑问题点如下:

    问题点1:if (minCapacity - elementData.length > 0),为啥不这样写
    if(minCapacity>elementData.length),效率高在哪?

    问题点2:MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;为啥是减8而不是其他,注释没看明白是啥意思


蝴蝶刀刀
浏览 365回答 1
1回答
随时随地看视频慕课网APP

相关分类

Java
我要回答