以下是String的部分源代码public final class String
implements java.io.Serializable, Comparable, CharSequence
{ private char value[]; private int offset; private int count; private int hash = 0; private static final long serialVersionUID = -6849794470754667710L;
/*这里是创建String的时候的源代码*/
public String(String original) {
this.count = original.count;
if (original.value.length > this.count) {
// 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.
this.value = new char[this.count];
System.arraycopy(original.value, original.offset,
this.value, 0, this.count);
} else {
// The array representing the String is the same
// size as the String, so no point in making a copy.
this.value = original.value;
}
}}我的问题就是this.count = original.count;
这个origianl.count有这样的用法?????
private int count;
count不是int变量吗?????
请求各位给个解释
implements java.io.Serializable, Comparable, CharSequence
{ private char value[]; private int offset; private int count; private int hash = 0; private static final long serialVersionUID = -6849794470754667710L;
/*这里是创建String的时候的源代码*/
public String(String original) {
this.count = original.count;
if (original.value.length > this.count) {
// 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.
this.value = new char[this.count];
System.arraycopy(original.value, original.offset,
this.value, 0, this.count);
} else {
// The array representing the String is the same
// size as the String, so no point in making a copy.
this.value = original.value;
}
}}我的问题就是this.count = original.count;
这个origianl.count有这样的用法?????
private int count;
count不是int变量吗?????
请求各位给个解释
* Initializes a newly created <code>String</code> object so that it
* represents the same sequence of characters as the argument; in other
* words, the newly created string is a copy of the argument string. Unless
* an explicit copy of <code>original</code> is needed, use of this
* constructor is unnecessary since Strings are immutable.
*
* @param original a <code>String</code>.
*/
用拷贝的方法初始化新创建的String,直接拷贝所传进来的参数。因为String是固定不可改变的,所以这个构造函数基本上是没用的。除非真的要明确地拷贝一个字符串。if (original.value.length > this.count)
也可写成
if (original.value.length > original.count)
可能就是为了看看传进来的新String所存的char[]的长度是不是与它的count相等
基本上没有用吧,只是为了保险
个人意见
用的就不是这个构造起吗?
如果是
this.count = original.count;
真能测试original的长度吗?
我和朋友讨论了一下,他说这里String是一个普通的类,也就是说此String非彼String
他的意思就是
这个时的String不过是
class String {
String() {
System.out.prinltn("I am not String");
}
}
一个普通的String
String original也是一个普通的类,
你同意这个说法吗?
我觉得一个类的中的
count必须要先被赴值
当你original.count的时候,才不至于nullponit异常,
如果是的话,这不是明显的nullponit吗?
当count为0,只是幅个值,
original.value.length > this.count
那么这句话永远都是ture
了?如果this.count = original.count;付的值,那么original的count是多少,0吗?
而且Java中String就是一个普通的类, 和C#不太一样
String s = new String("aaa");
original就是aaa对吧?
我的问题是,到底这个count是多少?
我还是描述一下我这段代吗的理解吧String s1 = new String();
这时候count是0
value是 (空白)
public String() {
value = new char[0];
}String s2 = new String("aaa");
"aaa"本身是一个String,但是他的count就是3吗?为什么?
如果是this.count = original.count,count就是3了
然后当 if (original.value.length > this.count) 为真,
this.value = new char[this.count];也就是说着时候value = new char[3],对吧。
然后System.arraycopy对吧。晕就晕"aaa"这里了,望各位帮忙"解"救谢啦
当"aaa"的时候,类被虚拟机载入,而"aaa"本身的属性count初始化0
当String s = new String("aaa");的时候,origial.count用的是那个0,所以this.count = 0
这就是我的理解
大错特错?????
_________________
我想应该是的,双引号括起来的内容估计JAVA虚拟机已经把它构造成一个一个完整的String类了,也就是,它内部的count和char[]已经被赋值且存在了,而当这个String变量"aaa"在被用来构造另一个String时,它的count与char[]应该是已经赋值过的
我也只能估计了,呵呵,不然的话无法解释
从灌蓝张飞这个名字上看,我觉得你打篮球时应该是中锋吧?呵呵,开个玩笑我觉得你的话有道理,但我想“JAVA虚拟机已经把它构造成一个一个完整的String类了”
他应该不是new构造器构造的,要不然构造其中的String original有是谁,那个count有事多少?
这就解释了为什么String str = new String("aaa");的时候为什么产生了2个String.
那么他就应该是用反射构造String?或这谋种特殊方式构造,然后"同时"确定count和value[]的对吗?
说明一下,我是打锋位摇摆人的,不是中锋,咱身高体重都达不到重量级的级别啊,
那么他就应该是用反射构造String?或这谋种特殊方式构造,然后"同时"确定count和value[]的对吗?
——————————————
我想既然JAVA里的String的构造函数那么多,而且构造的手段又那么多,那么我们就不能老用一个构造String的方法来衡量所有的构造手段,当然,一些好理解,好构造的手段Sun给我们解释清楚了,但是一些特殊的,不常用的构造过程,Sun肯定是为了不费口舌而“封装”,“隐藏”掉了,而留下让我们用常理的思考却百思不得其解
我记得JAVA里有“字符串池”这个概念的吧,如果有两个字符串“NBA”与“CBA”,在JVAVA里它会保存“NBAC”而不是资源浪费地保存两个“BA”,JAVA能把字符串池里的字符资源区分出一个一个的字符串“NBA”与“CBA”,肯定有它一套机制,而这套机制目前我们是不太清楚的。这样推测下来,String str = new String("aaa");也是有我们不太了解的处理过程的。至于count和value[],我想这对于"NBA"与“CBA”这样的字符串常量来说也是不难得到的,本身String还与char[]有着不可分割的联系,“NBA”实际上也被JAVA看作'N','B','A'吧
我不清楚是不是《深入JAVA虚拟机》能给我们揭露这些细节,我也没看过,呵呵
iknowurcode()还得找找资料才行吧
public static void main(String args[]) throws Exception {
String str = "aaaaa";
Class a = str.getClass();
Field f = a.getDeclaredField("count");
f.setAccessible(true);
System.out.println(f.getInt(str));
}
}
}