如何使用“”来初始化字符串? &QUOT ;? [英] How can a string be initialized using " "?

查看:113
本文介绍了如何使用“”来初始化字符串? &QUOT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果String就像其他任何类一样,如何使用双引号初始化?

If String is a class just like any other, how can it be initialized using double quotes?

推荐答案

Java字符串特殊


Java的设计者决定在面向对象的语言中保留原始类型,而不是将所有东西都作为对象,以便提高语言的性能。基元存储在调用堆栈中,这需要较少的存储空间并且操作起来更便宜。另一方面,对象存储在程序堆中,这需要复杂的内存管理和更多的存储空间。

The designers of Java decided to retain primitive types in an object-oriented language, instead of making everything an object, so as to improve the performance of the language. Primitives are stored in the call stack, which require less storage spaces and are cheaper to manipulate. On the other hand, objects are stored in the program heap, which require complex memory management and more storage spaces.

出于性能原因,Java的String设计在介于两者之间一个原语和一个类。

For performance reason, Java's String is designed to be in between a primitive and a class.

例如

String s1 = "Hello";              // String literal
String s2 = "Hello";              // String literal
String s3 = s1;                   // same reference
String s4 = new String("Hello");  // String object
String s5 = new String("Hello");  // String object

注意: 字符串文字存储在公共游泳池。这有助于共享具有相同内容的字符串的存储空间以节省存储空间。 字符串通过new运算符分配的对象存储在中,并且没有共享相同内容的存储空间。

Note : String literals are stored in a common pool. This facilitates sharing of storage for strings with the same contents to conserve storage. String objects allocated via new operator are stored in the heap, and there is no sharing of storage for the same contents.

这篇关于如何使用“”来初始化字符串? &QUOT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆