对了,看了关于一些贴之后,我觉得我要知道的是:
1 string对象创造的过程是怎么回事?如果是string x ="aa";这样,是使用引用一个string对象,然后在那里(常量池??我不知道)寻找是否有相同内容的string对象,有,则传一个引用给所需引用的对象,没有,自动创造一个,然后返回地址给此引用变量;是这样吗?可是对于"hel"+"lo"来说是怎么产生"hello"的呢?有没有用到stringbuffer?看了JDK的stringbuffer.tostring()
public String toString() {
return new String(this);//返回了一个新的string对象
}
对于"hel"+"lo"和hel+lo有什么区别??谢谢大家,谢谢高手,新年快乐!!!
敬!!!!!!!!!!!!!
1 string对象创造的过程是怎么回事?如果是string x ="aa";这样,是使用引用一个string对象,然后在那里(常量池??我不知道)寻找是否有相同内容的string对象,有,则传一个引用给所需引用的对象,没有,自动创造一个,然后返回地址给此引用变量;是这样吗?可是对于"hel"+"lo"来说是怎么产生"hello"的呢?有没有用到stringbuffer?看了JDK的stringbuffer.tostring()
public String toString() {
return new String(this);//返回了一个新的string对象
}
对于"hel"+"lo"和hel+lo有什么区别??谢谢大家,谢谢高手,新年快乐!!!
敬!!!!!!!!!!!!!
解决方案 »
- ---------------->关于递归的疑惑<-----------------
- JAVA SE、MyEclipse6.0下载地址,要的抓紧时间下!
- 由于request没有 s e t P a r a m e t e r方法
- 关于方法的泛型参数,知道的进来讨论一下,看看有没有这个功能
- 如何输入一个小写字母将其变成大写的显示出来?
- 线程如何唤醒
- Java 入门遇到困难
- 菜鸟问题:如何得到开发帮助?
- 关于读文件内容,问题出在哪里?
- 求教个问题 求高手解答 谢谢
- 求解:non-static method setTitle(java.lang.string ) cannot be reference from a static context
- 求助关于javac HelloWorld.java问题
System.out.println(hello=="hello");
System.out.println(hello.equals(hel.concat(lo)));
结果就为true了。
// Home Page : http://members.fortunecity.com/neshkov/dj.html - Check often for new version!
// Decompiler options: packimports(3)
// Source File Name: test.javaimport java.io.PrintStream;public class test
{ public test()
{
} public static void main(String args[])
{
String s = "hello";
String s1 = "hel";
String s2 = "lo";
System.out.println(s == "hello");
System.out.println(s == (new StringBuilder()).append(s1).append(s2).toString());
}
}
System.out.println(hello==hel+lo);//为FALSE的原因是因为hello与hel+lo虽然值一样,但是不是同一个对象,也就是说指向的内存地址不在同一个空间
上面的问题不同的编译器实现有可能不同,会出不同的结果,就没有必要死钻牛角尖了
另外,
String hello="hello";
String hel="hel";
String lo="lo";
String h = new String("hello");
System.out.println(hello=="hel"+"lo");//文字字符传常量
System.out.println(hello==hel+lo);//字符传变量
System.out.println(hello=="hello");
System.out.println(h=="hello");结果就是
true
false
true
false因为 String hello="hello";这个JAVA在内存里创建了“hello"对象。hello这个变量指向这个对象。
因为是LITERAL STRING。所以"hel"+"lo"生成时就是已经存在的"hello"对象。
String h = new String("hello");在JAVA API里解释说是新生成了2个STRING 对象。
一个是遇到"hello"这个LITERAL STRING时,一个是在NEW 的时候。最后说一下楼上的观点。不管什么编译器,只要是JAVA的,就不可能不同结果。对于对象,不是基本类型 ,==是判断是否引用同一个对象。EQUALS()判断是否值相同。
今天看了大家的讨论收益非浅!
总结一下:
啊木说的对前者是常量能在编译时刻就确定的,所以第一个式子是true;
而第2个则是运行时才能确定的,另外谈一下:那么关于string(常量对象)是不是涉及有对象改变的情况就自动调用Stingbuffer在产生运算结果后使用tostring来返回给一个string对象
public String toString() {
return new String(this);
}
正如“漂泊的叶子”所做的反编译,看到的一些情况
System.out.println(s == (new StringBuilder()).append(s1).append(s2).toString());