刚看了一个帖子 是这样一个问题 String str=new String ("hello");会创建几个对象的问题。
现在我想说下自己的解答 我的答案是 一个或者两个
首先常量池 CONSTANT_String_info jvm规范中这样定义。 固定长度的该表中用来存储文字字符串值,该值也可以表示类
java.lang.String的实例
再来看看String 的构造方法
/**
* Initializes a newly created {@code String} 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} is needed, use of this constructor is
* unnecessary since Strings are immutable.
*
* @param original
* A {@code 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;
}String 的构造方法中也是一个String 对象。所以给出之前的解答 。如果”hello“对象在本段代码执行之前并没有出现在 CONSTANT_String_info表中则在 String池中创建对象Hello ,同时在堆中创建对象”hello“ 这时 就是创建了两个对象 如果再次之前hello已经出现在了常量池中,则只需撞见一个对象。另外注意有些人说对象创建在栈中。纯属坑爹,栈中只保存对对象的引用,不保存对象。 大家觉得呢?
如果
现在我想说下自己的解答 我的答案是 一个或者两个
首先常量池 CONSTANT_String_info jvm规范中这样定义。 固定长度的该表中用来存储文字字符串值,该值也可以表示类
java.lang.String的实例
再来看看String 的构造方法
/**
* Initializes a newly created {@code String} 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} is needed, use of this constructor is
* unnecessary since Strings are immutable.
*
* @param original
* A {@code 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;
}String 的构造方法中也是一个String 对象。所以给出之前的解答 。如果”hello“对象在本段代码执行之前并没有出现在 CONSTANT_String_info表中则在 String池中创建对象Hello ,同时在堆中创建对象”hello“ 这时 就是创建了两个对象 如果再次之前hello已经出现在了常量池中,则只需撞见一个对象。另外注意有些人说对象创建在栈中。纯属坑爹,栈中只保存对对象的引用,不保存对象。 大家觉得呢?
如果
对于String类型变量的赋值,遵循下面的规则,java中有String pool(我估计是由于String对象的不变性,所以java中有String pool)。
java中有两种赋值方式,一种采用字面值方式的赋值,String s = "aaa"; 查找String pool中是否存在“aaa”这个对象,如果不存在,则在String pool 中创建一个"aaa"对象,然后将String pool中这个对象的地址返回给s这个引用,这样s会指向String pool 中"aaa"这个对象。如果String pool中已经有这个“aaa”对象时java虚拟机不会创建新的对象,而是将这个对象的地址直接返回。
另一种方式是String s = new String("aaa"); 首先在String pool 中查找有没有这个对象,如果已经有了,则不再String pool中创建了,而是在堆中创建该对象,然后返回堆中该对象的地址。如果没有,则在String pool中创建该对象,并且在堆中也创建该对象,最后返回堆中该对象的地址给s。http://blog.csdn.net/daijope/article/details/6559531
new 出来的放在堆中的,引用是放在栈中的吧! 所以就是酱紫,要么一个,要么两个。我想,,,,
正解
一个String对象S 引用了一个地址也就是字符串对象。
所以一共是两个对象。
如果还有什么探讨的,我给你推荐一个java技术交流群,145152031
,、应该可以满足你
String str=new String ("hello"); 创建2个对象 一个是hello对象 一个是str指向应用对象