String类 ,是基本数据类型,也可以作为引用数据类型,2种方式的计算长度的方法是一样,可具体是如何实现length()方法的  ,是根据什么实现的 实现的位置,是虚拟机处理 内存处理, 还是String类中实现??

解决方案 »

  1.   

    看看C的吧,原理是一样的
    int length(const char *str) {
        int len = 0;
        
        while (*str++) {
            ++len;
        }    return len;
    }
      

  2.   

    String内部是char数组public int length() {
            return count;
    }
      

  3.   

    是String类实现的
    虚拟机不会提供方法处理,只会解析java指令,内存更不可能自己处理,只是存储而已
    String类内部是用字符数组保存数据的,长度就直接取该数组的长度
      

  4.   

    string.length 即可,也可以通过int length(const char *str) {
      int len = 0;
        
      while (*str++) {
      ++len;
      }  return len;
    }
      

  5.   

    对。。String.length()函数可以计算可以下载帮助文档
      

  6.   

    String类型当然不是基本数据类型了
      

  7.   

    String   length()方法,返回  count,count 是如何来的,我看了String类 ,里面只是定义了一个count,没有赋值,我要的是实现原理,怎么计算长度的,要的不是一个封装好的方法,方法我也会用的,把封装解开后的内容是什么,才是我想要的
      

  8.   


    String是不可变类,当然是从构造函数来的,实现原理就去源码看构造函数:
    public String() {
    this.offset = 0;
    this.count = 0;
    this.value = new char[0];
        }public String(String original) {
    int size = original.count;
    char[] originalValue = original.value;
    char[] v;
       if (originalValue.length > size) {
          // The array representing the String is bigger than the new
          // String itself.  Perhaps this constructor is being called
          // in order to trim the baggage, so make a copy of the array.
                int off = original.offset;
                v = Arrays.copyOfRange(originalValue, off, off+size);
      } else {
          // The array representing the String is the same
          // size as the String, so no point in making a copy.
        v = originalValue;
      }
    this.offset = 0;
    this.count = size;
    this.value = v;
        } public String(char value[]) {
    this.offset = 0;
    this.count = value.length;
    this.value = StringValue.from(value);
        }
     public String(char value[], int offset, int count) {
            if (offset < 0) {
                throw new StringIndexOutOfBoundsException(offset);
            }
            if (count < 0) {
                throw new StringIndexOutOfBoundsException(count);
            }
            // Note: offset or count might be near -1>>>1.
            if (offset > value.length - count) {
                throw new StringIndexOutOfBoundsException(offset + count);
            }
            this.offset = 0;
            this.count = count;
            this.value = Arrays.copyOfRange(value, offset, offset+count);
        }
      

  9.   

    你疑惑你妈逼,就你狗日的智商还学java呢,吃屎去吧你!