Java中的字符串和字符数组 [英] String and Character Array in Java
问题描述
我是刚刚从C ++转移到Java的学生。
I am a student who has just shifted from C++ to Java.
在Java中,为Strings定义单独的数据类型的主要原因是什么, Char数组?两者之间的区别是什么?
由于我只学习C ++,直到现在我还是感觉他们是一样的。请尽可能澄清。
Since I have only studied C++, up till now I was under the impression that they are the same thing. Please clarify if possible.
推荐答案
字符串
是不可变的。 Char
数组不是。一个字符串是用下面的char数组实现的,但是每次你尝试修改它(比如连接,替换等),它会给你一个新的 String
object。
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 new String
object.
因此, String
表现为常量 Char
数组,但是带有一些语法糖,这也使得它们更容易使用。例如, +
运算符已作为字符串连接运算符重载。
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 overloaded as a string concatenation operator as well.
这篇关于Java中的字符串和字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!