stringBuilder的父类到底是啥?

just_want_to 2017-04-19 02:30:22

-----------------------

--------------------------

如图,StringBuffer的父类源码和jdk标明的不一样?而且AbstractStringBuilder这个类在jdk中查不到?
...全文
706 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
古琴台何 2017-05-06
  • 打赏
  • 举报
回复
在jdkapi上面是有AbstractStringBuilder这个抽象父类的,abstract class AbstractStringBuilder implements Appendable, CharSequence
110成成 2017-04-28
  • 打赏
  • 举报
回复
查看源码 public final class StringBuilder extends AbstractStringBuilder implements java.io.Serializable, CharSequence Object 是所有类的父类。
爱摸鱼de老邪 2017-04-28
  • 打赏
  • 举报
回复
以后这种问题自己可以attach源码看看。

/*
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */

package java.lang;

import sun.misc.FloatingDecimal;
import java.util.Arrays;

/**
 * A mutable sequence of characters.
 * <p>
 * Implements a modifiable string. At any point in time it contains some
 * particular sequence of characters, but the length and content of the
 * sequence can be changed through certain method calls.
 *
 * @author      Michael McCloskey
 * @author      Martin Buchholz
 * @author      Ulf Zibis
 * @since       1.5
 */
abstract class AbstractStringBuilder implements Appendable, CharSequence {
    /**
     * The value is used for character storage.
     */
    char[] value;

    /**
     * The count is the number of characters used.
     */
    int count;

    /**
     * This no-arg constructor is necessary for serialization of subclasses.
     */
    AbstractStringBuilder() {
    }

    /**
     * Creates an AbstractStringBuilder of the specified capacity.
     */
    AbstractStringBuilder(int capacity) {
        value = new char[capacity];
    }

    /**
     * Returns the length (character count).
     *
     * @return  the length of the sequence of characters currently
     *          represented by this object
     */
    public int length() {
        return count;
    }

    /**
     * Returns the current capacity. The capacity is the amount of storage
     * available for newly inserted characters, beyond which an allocation
     * will occur.
     *
     * @return  the current capacity
     */
    public int capacity() {
        return value.length;
    }

    /**
     * Ensures that the capacity is at least equal to the specified minimum.
     * If the current capacity is less than the argument, then a new internal
     * array is allocated with greater capacity. The new capacity is the
     * larger of:
     * <ul>
     * <li>The <code>minimumCapacity</code> argument.
     * <li>Twice the old capacity, plus <code>2</code>.
     * </ul>
     * If the <code>minimumCapacity</code> argument is nonpositive, this
     * method takes no action and simply returns.
     *
     * @param   minimumCapacity   the minimum desired capacity.
     */
    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);
    }

    /**
     * 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);
        }
    }

    ……

}
liuxing9345 2017-04-20
  • 打赏
  • 举报
回复 1
继承自Object的那个你是从docs文档里面看到的吗?应该是文档写错了,源码里面的应该是继承自AbstractStringBuilder类的,不是在jdk中找不到这个AbstractStringBuilder类,而是在文档中找不到,你的文档有问题,jdk不可能没有。你不要把jdk和文档搞混淆了
小灰狼 2017-04-20
  • 打赏
  • 举报
回复
引用 4 楼 hemowolf 的回复:
[quote=引用 2 楼 pany1209 的回复:] AbstractStringBuilder在rt.jar的java.lang里面。。。jdk文档没写这个抽象类。。。是StringBuffer和stringBuilder的父类
StringBuilder 和 StringBuffer 都是直接继续自 Object,跟那什么抽象类没关系[/quote] 晕死 JDK 的文档里说,StringBuilder 和 StringBuffer 都是直接继承自 Object,但打开这两个类的源代码,则都是继承自 AbstractStringBuilder。 当然,可能这个AbstractStringBuilder 不是公共类,所以没有出现在API 文档里
李德胜1995 2017-04-20
  • 打赏
  • 举报
回复
引用 4 楼 hemowolf 的回复:
[quote=引用 2 楼 pany1209 的回复:] AbstractStringBuilder在rt.jar的java.lang里面。。。jdk文档没写这个抽象类。。。是StringBuffer和stringBuilder的父类
StringBuilder 和 StringBuffer 都是直接继续自 Object,跟那什么抽象类没关系[/quote] 自己去源码。。。。
小灰狼 2017-04-20
  • 打赏
  • 举报
回复
引用 2 楼 pany1209 的回复:
AbstractStringBuilder在rt.jar的java.lang里面。。。jdk文档没写这个抽象类。。。是StringBuffer和stringBuilder的父类
StringBuilder 和 StringBuffer 都是直接继续自 Object,跟那什么抽象类没关系
李德胜1995 2017-04-19
  • 打赏
  • 举报
回复
AbstractStringBuilder在rt.jar的java.lang里面。。。jdk文档没写这个抽象类。。。是StringBuffer和stringBuilder的父类
小灰狼 2017-04-19
  • 打赏
  • 举报
回复
JDK 里没有 AbstractStringBuilder 这个类 楼主的第一个图片的信息从哪里来的?

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧