Java 中的字符串和字符数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18338629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String and Character Array in Java
提问by iluvthee07
I am a student who has just shifted from C++ to Java.
我是一个刚从 C++ 转向 Java 的学生。
In Java what could be the main reason for defining separate data types for Strings and Char arrays? What is the difference between the two?
在 Java 中,为 Strings 和 Char 数组定义单独的数据类型的主要原因是什么?两者有什么区别?
Since I have only studied C++, up till now I was under the impression that they are the same thing. Please clarify if possible.
因为我只学过 C++,所以到现在我的印象是它们是一回事。如果可能,请澄清。
采纳答案by Ravi Thapliyal
String
is immutable. Char
array is not. A string is implemented with a char array underneath but every time you try to modify it (like with concatenation, replace etc.) it gives you a newString
object.
String
是不可变的。Char
数组不是。一个字符串是用下面的 char 数组实现的,但每次尝试修改它(如连接、替换等)时,它都会给你一个新String
对象。
So, String
behaves as a constant Char
array but comes with certain syntactic sugar that also makes them very easier to use. For example, the addition +
operator has been overloadedas a string concatenation operator as well.
因此,它String
表现为一个常量Char
数组,但带有某些语法糖,这也使它们更易于使用。例如,加法+
运算符也被重载为字符串连接运算符。
回答by Philipp Sander
回答by chrylis -cautiouslyoptimistic-
In Java, String
is a basic system class that essentially wraps a char[]
. There are several reasons why, for most uses, having a full class is preferable to directly handling arrays:
在 Java 中,String
是一个基本的系统类,它本质上包装了一个char[]
. 对于大多数用途,拥有完整类比直接处理数组更可取的原因有以下几个:
String
s are immutable; once you have a reference to someString
, you know it's never going to change.String
s provide useful methods that a bare array couldn't, such aslength()
, and have clearly-defined comparison semantics.- You never have to deal with string termination yourself.
- Java has a special exception for the rule of "no operator overloading" to support string concatenation (with
+
).
String
s 是不可变的;一旦你引用了 someString
,你就会知道它永远不会改变。String
s 提供了裸数组无法提供的有用方法,例如length()
, 并具有明确定义的比较语义。- 您永远不必自己处理字符串终止。
- Java 对“无运算符重载”规则有一个特殊的例外,以支持字符串连接(使用
+
)。
Essentially, it's good OO practice to use a class to collect the desired behavior and the data structures in the same place, and String
wraps up an array of characters with the useful operations that you want to perform on a string.
本质上,使用类在同一位置收集所需的行为和数据结构,并String
用您想要对字符串执行的有用操作封装字符数组是一种很好的 OO 实践。
回答by itsNotABlanket
The advantage to using the string object is all the methods available to it. For example:
使用字符串对象的优点是它可以使用所有方法。例如:
stringExample1.equals(stringExample2);
String stringExample3 = stringExample1.replace(substring1, substring2);
回答by Peter Walser
There is a semanticdifference. Just because data is stored the same way, this doesn't mean it's the same thing. Dates
and Amounts
may also have the same internal representation (long
for a timestamp or fixed point amount of cash), but they're not the same. The char
array could as well mean a 16-bit image.
存在语义差异。仅仅因为数据以相同的方式存储,这并不意味着它们是相同的。Dates
并且Amounts
也可能具有相同的内部表示(long
对于时间戳或定点现金),但它们并不相同。该char
数组也可以表示 16 位图像。
In object orientation, it's good practice to model objects based on what they are and can, and not by how they internally store their data. This allows you to encapsulatethe data (and restrict or control (observer support) access with getters/setters, or even make the internal representation immutable or poolable), and provide appropriate methodsfor your objects.
在面向对象中,最好根据对象是什么和可以做什么来建模对象,而不是根据它们在内部存储数据的方式。这允许您封装数据(并使用 getter/setter 限制或控制(观察者支持)访问,甚至使内部表示不可变或可池化),并为您的对象提供适当的方法。
回答by Santosh Kumar
String is immutable in Java and stored in the String pool. Once it is created it stays in the pool until garbage collected.Since, String is immutable , the logging password is as readable string.It has greater risk of producing the memory dump to find the password.
String 在 Java 中是不可变的,并存储在 String 池中。一旦它被创建,它就会留在池中直到垃圾收集。由于 String 是不可变的,因此日志密码是可读的字符串。它产生内存转储以找到密码的风险更大。
where as Char array is created in heap and you can override with some dummy values.
其中 Char 数组是在堆中创建的,您可以使用一些虚拟值进行覆盖。