字符串不是值类型,但我们仍然使用它的值类型。那么,string s;
编译成类似string s = new String(..);
?是“string s;”编译为“string s = new String(..);”?
0
A
回答
2
当您定义string s;
时,您只能定义一个引用,它当前根本没有指向任何内容。由于字符串是引用类型,因此编译器不会生成string s = new String(..);
。你可能会理解它,string s = null;
将是编译结果。
对于值类型,例如int,情况是不同的。例如,当您定义int i;
时,它将编译为int i = 0;
,其中0是默认值。
0
我想通了我的困惑:在阅读this后,我明白堆栈将持有参考s
将在初始化后分配在堆中。
+0
我不知道谁投了票,但你的理解比你在问题中表达的要好得多。但是,它仍然只涵盖了一些字符串之谜。还有另一个重要的事情,使字符串特殊,http://msdn.microsoft.com/en-us/library/system.string.isinterned.aspx – 2012-03-11 11:05:33
相关问题
- 1. String s =“something”;和String s = new String(“something”);
- 2. 声明String s = new String(“abc”)。intern(); over String s =“abc”(反之亦然)
- 3. `string s(“hello”);```string s =“hello”;`
- 4. 在Java中,是String s = new String()有没有用?
- 5. string s; &s+1;法律? UB?
- 6. 什么时候应该使用String s = new String(“Hello World”)?
- 7. python string u“%(word)s”
- 8. Is String s =“foobar”atomic?
- 9. writeUTF(String s)将VS
- 10. Double.valueOf(String s)和Double.ParseDouble(String s)有什么区别?
- 11. main(String ... s)和main(String [] s)有什么区别?
- 12. 为什么string str = new string(“abc”)不能通过编译器?
- 13. android getSystemService(String s) - 反射?
- 14. 如何使用System.Web.UI.Page.DecryptString(String s)?
- 15. cordova store fileEntry name to string
- 16. TextView String(s)不出现
- 17. 试图将元素添加到列表<String[,]> S = new List <String[,]>();
- 18. std :: string s()奇怪的行为
- 19. 爪哇 - URLDecoder.decode(String s)将VS URLDecoder.decode(一个String,字符串ENC)
- 20. className.main(new String [] {“filename.txt”})?
- 21. force assertEquals(String s,Object o)使用o.toString()?
- 22. C#中的Integer.parseInt(String s,int radix)
- 23. Java,泛型:Set <?> s = HashSet <String>()和Set s = HashSet <String>()之间的区别是什么?
- 24. String和new String有什么区别?
- 25. 我无法使用Color.FromName(string s)方法
- 26. 对String [] s的列表进行排序?
- 27. 调用Application.method(String s)将方法从index.scala.html
- 28. 在Android中String foo =“bar”与String foo = new String(“bar”)?
- 29. 这两个初始化有什么区别:Object x = new String(); String x = new String();
- 30. 可能的Hashtable <Integer,String,String> hashtbl = new Hashtable <Integer,String,String>();
我不这么认为。 – BoltClock 2012-03-11 10:13:55
当你做'string s;'时,没有任何东西被实例化。 – 2012-03-11 10:14:32
但是,如果它不是一个值类型,它必须用'new'实例化,不是吗? – theateist 2012-03-11 10:15:36