String s= {"123141231"}
与
String s= new String("123141231")
什么区别?
与
String s= new String("123141231")
什么区别?
解决方案 »
- 紧急求助,关于NumberFormat小数点问题
- JAVA从新到主要搞WEB的步骤?
- 在Swing程序中可以调用MS Word 的 OLE 控件 在程序中直接打开word嘛?
- 为什么double类型的变量除零不会产生异常
- 菜鸟问题:使用JDBC访问本机ACCESS数据库,出现不能访问的问题,请教大侠!!!
- 一点语法问题,各位达人都近来看呀!
- 请问为什么JB6里如何放可视控件大小,位置呢,谢谢(初次学习)
- 兔子问题,哪位高手能用递归做出来。。。
- jvm是什么?
- intelliJ IDEA 2.6的汉字半角问题
- JAVA 压缩 文件夹的 问题~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- 怎样能得到对象的属性名和属性值?
可以吗?
应该是
String s= "123141231";
吧?首先,“s”是个变量名,即“引用”,"123141231"则表示一个字符串常量。而常量本身也能代表一个运行时的对象。所以
String s = "123141231";
只是让变量s引用到一个字符串对象(即常量"123141231"所代表的那个),而“String s = new String("123141231")”,则是利用"123141231"作参数,首先构造一个新的String对象,然后让s引用到它。
所以后者比前者多产生了一个冗余对象。
String s= new String("123141231") 两个对象
String s = "Test";
The above construct is equivalent to, but more efficient than, this one, which ends up creating two Strings instead of one:
String s = new String("Test");
The compiler creates the first string when it encounters the literal string "Test", and the second one when it encounters new String.
source: http://java.sun.com/docs/books/tutorial/java/data/stringsAndJavac.htmlAdvice: If you are C/C++ programmer be careful with shortcut assignment operator "+=" in Java when you work with Strings!Why: The shortcut assignment operator += when used with Strings may confuse C and C++ programmers at first. Recall that a += b is equivalent to a = a + b. Let's look at two codesamples written in C++ and the Java programming language:
//C++ code
string* s1 = new string("hello");
string* s2 = s1;
(*s1) += " world";
cout<<*s1<<endl<<*s2<<endl;
return 0;
//s1 = s2 = "hello world"//Java programming language code
String s1 = "hello";
String s2 = s1;
s1 += " world";
System.out.println(s1 + "\n" + s2);
//s1 = "hello world" and s2 = "hello"In the C++ example, the strings s1 and s2 print the same result because they both point to the same address. In the Java programming language, Strings can't be modified, so the + operator must create a new String when "world" is appended to s1.source: http://java.sun.com/docs/books/tutorial/java/data/stringsAndJavac.html
好像也是一个对象吧,
在JAVA中,相同的字符对象不是放在HEAD中的吗??
强制产生一个新对象
String s="123141231";
s会引用内存中已有的字符串
例如
String s1=new String("abc");
String s2=new String("abc");
则
s1!=s2,s1.equals(s2)=true;
若
String s1="abc";
String s2="abc";
则
s1==s2,s1.equals(s2)=true;