标签:jvm stringbuffer java trimtosize setlength
首先声明jdk-version:7u40
好了,先新建一个StringBuffer.
StringBuffer aa = new StringBuffer("12345");下面是jdk源码:
public StringBuffer(String str) { super(str.length() + 16); append(str); }
AbstractStringBuilder(int capacity) { value = new char[capacity]; }
StringBuffer aa = new StringBuffer("12345"); System.out.println(aa.capacity());//21 System.out.println(aa.length());//5
aa.setLength(16);
System.out.println(aa.capacity());//21 System.out.println(aa.length());//16
其中16-5多出来的被`\0`补全了.
输出:
System.out.println(aa.toString());
public void setLength(int newLength) { if (newLength < 0) throw new StringIndexOutOfBoundsException(newLength); ensureCapacityInternal(newLength); if (count < newLength) { for (; count < newLength; count++) value[count] = '\0'; } else { count = newLength; } }下面这句确认是否新设置的长度,大于现有char[]的长度,大于就要扩展容量.
ensureCapacityInternal(newLength);
public void ensureCapacity(int minimumCapacity) { if (minimumCapacity > 0) ensureCapacityInternal(minimumCapacity); } /** * This method has the same contract as ensureCapacity, but is * never synchronized. */ private void ensureCapacityInternal(int minimumCapacity) { // overflow-conscious code if (minimumCapacity - value.length > 0) expandCapacity(minimumCapacity); } /** * This implements the expansion semantics of ensureCapacity with no * size check or synchronization. */ void expandCapacity(int minimumCapacity) { int newCapacity = value.length * 2 + 2; if (newCapacity - minimumCapacity < 0) newCapacity = minimumCapacity; if (newCapacity < 0) { if (minimumCapacity < 0) // overflow throw new OutOfMemoryError(); newCapacity = Integer.MAX_VALUE; } value = Arrays.copyOf(value, newCapacity); }至于扩容...2倍+2
如果你设置的长度,小于现有的数据长度,也就是
aa.setLength(2);那么输出:
System.out.println(aa.capacity());//21 System.out.println(aa.length());//2 System.out.println(aa.toString());//12容量没变,记录char[]长度的成员变量值变了,至于value[]里的值并没有消失.证据是:
我调用了:
aa.append("aa");然后打断点...
得到下面两张图:
由此可见,不仅没消失,append只是复写了它,也就是如果可以得到count的修改权,我们能找回以前的数据...不过,反正我没得到,你办到了记得通知我.
我们继续说trimToSize,解释是它会将value[](也就是char[])的长度(容量)改成数据的长度
调用:
StringBuffer aa = new StringBuffer("12345"); aa.setLength(2); aa.append("aa"); aa.trimToSize(); System.out.println(aa.capacity());//4 System.out.println(aa.length());//4
/** * Attempts to reduce storage used for the character sequence. * If the buffer is larger than necessary to hold its current sequence of * characters, then it may be resized to become more space efficient. * Calling this method may, but is not required to, affect the value * returned by a subsequent call to the {@link #capacity()} method. */ public void trimToSize() { if (count < value.length) { value = Arrays.copyOf(value, count); } }
/** * Copies the specified array, truncating or padding with null characters (if necessary) * so the copy has the specified length. For all indices that are valid * in both the original array and the copy, the two arrays will contain * identical values. For any indices that are valid in the copy but not * the original, the copy will contain <tt>'\\u000'</tt>. Such indices * will exist if and only if the specified length is greater than that of * the original array. * * @param original the array to be copied * @param newLength the length of the copy to be returned * @return a copy of the original array, truncated or padded with null characters * to obtain the specified length * @throws NegativeArraySizeException if <tt>newLength</tt> is negative * @throws NullPointerException if <tt>original</tt> is null * @since 1.6 */ public static char[] copyOf(char[] original, int newLength) { char[] copy = new char[newLength]; System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); return copy; }
/** * Copies an array from the specified source array, beginning at the * specified position, to the specified position of the destination array. * A subsequence of array components are copied from the source * array referenced by <code>src</code> to the destination array * referenced by <code>dest</code>. The number of components copied is * equal to the <code>length</code> argument. The components at * positions <code>srcPos</code> through * <code>srcPos+length-1</code> in the source array are copied into * positions <code>destPos</code> through * <code>destPos+length-1</code>, respectively, of the destination * array. * <p> * If the <code>src</code> and <code>dest</code> arguments refer to the * same array object, then the copying is performed as if the * components at positions <code>srcPos</code> through * <code>srcPos+length-1</code> were first copied to a temporary * array with <code>length</code> components and then the contents of * the temporary array were copied into positions * <code>destPos</code> through <code>destPos+length-1</code> of the * destination array. * <p> * If <code>dest</code> is <code>null</code>, then a * <code>NullPointerException</code> is thrown. * <p> * If <code>src</code> is <code>null</code>, then a * <code>NullPointerException</code> is thrown and the destination * array is not modified. * <p> * Otherwise, if any of the following is true, an * <code>ArrayStoreException</code> is thrown and the destination is * not modified: * <ul> * <li>The <code>src</code> argument refers to an object that is not an * array. * <li>The <code>dest</code> argument refers to an object that is not an * array. * <li>The <code>src</code> argument and <code>dest</code> argument refer * to arrays whose component types are different primitive types. * <li>The <code>src</code> argument refers to an array with a primitive * component type and the <code>dest</code> argument refers to an array * with a reference component type. * <li>The <code>src</code> argument refers to an array with a reference * component type and the <code>dest</code> argument refers to an array * with a primitive component type. * </ul> * <p> * Otherwise, if any of the following is true, an * <code>IndexOutOfBoundsException</code> is * thrown and the destination is not modified: * <ul> * <li>The <code>srcPos</code> argument is negative. * <li>The <code>destPos</code> argument is negative. * <li>The <code>length</code> argument is negative. * <li><code>srcPos+length</code> is greater than * <code>src.length</code>, the length of the source array. * <li><code>destPos+length</code> is greater than * <code>dest.length</code>, the length of the destination array. * </ul> * <p> * Otherwise, if any actual component of the source array from * position <code>srcPos</code> through * <code>srcPos+length-1</code> cannot be converted to the component * type of the destination array by assignment conversion, an * <code>ArrayStoreException</code> is thrown. In this case, let * <b><i>k</i></b> be the smallest nonnegative integer less than * length such that <code>src[srcPos+</code><i>k</i><code>]</code> * cannot be converted to the component type of the destination * array; when the exception is thrown, source array components from * positions <code>srcPos</code> through * <code>srcPos+</code><i>k</i><code>-1</code> * will already have been copied to destination array positions * <code>destPos</code> through * <code>destPos+</code><i>k</I><code>-1</code> and no other * positions of the destination array will have been modified. * (Because of the restrictions already itemized, this * paragraph effectively applies only to the situation where both * arrays have component types that are reference types.) * * @param src the source array. * @param srcPos starting position in the source array. * @param dest the destination array. * @param destPos starting position in the destination data. * @param length the number of array elements to be copied. * @exception IndexOutOfBoundsException if copying would cause * access of data outside array bounds. * @exception ArrayStoreException if an element in the <code>src</code> * array could not be stored into the <code>dest</code> array * because of a type mismatch. * @exception NullPointerException if either <code>src</code> or * <code>dest</code> is <code>null</code>. */ public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);不过value的引用,指向已经替换成了copyOf回来的,那原数组也就是失去了引用,也就是不可达了吧,是吧?你也觉得它是等垃圾车了吧.
至于它是不是所谓的GC Roots对象,和GC Roots是否会被回收,我没有深究,如果你知道,请评论告诉我.
到此,就是我想记录的关于标题提到的两个方法,相关的事项.希望你能看完,虽然并没有什么卵用.
版权声明:本文为博主原创文章,未经博主允许不得转载。
关于StringBuffer.setLength和trimToSize
标签:jvm stringbuffer java trimtosize setlength
原文地址:http://blog.csdn.net/java1234321/article/details/47356539