日期:2014-05-20  浏览次数:20731 次

关于java常量存储方式的问题,大家帮忙看一下
最近学习java,有个String的小问题请教一下大伙,大家帮忙看一下!
String str=new String("Hello");
System.out.prinln(str.length());//输出为5
本人查了一下String的源码,有如下几个构造方法:
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[]) {
int size = value.length;
this.offset = 0;
this.count = size;
this.value = Arrays.copyOf(value, size);
  }

public int length() {
  return count;
  }
对于str.length(),返回的是Count的值即字符串的长度,我想问的是对于new String("Hello")调用的是哪个构造方法,如果调用的是String(String original),那么在这个函数中Hello的长度是怎么来的,编译器如何解释这部分,大虾们帮忙看一下!!!

------解决方案--------------------
调用的肯定是String(String original)
int size = original.count;
this.count = size;
这两句就是把长度赋值了啊