+ Java 中字符串的运算符
几分钟前我看到了这个问题,并决定在java String类中查看一下如果 +
运算符有一些重载。
我找不到任何东西,但我知道我可以做到这一点
String ab = "ab";
String cd = "cd";
String both = ab + cd; //both = "abcd"
在哪里实现的?
I saw this question a few minutes ago, and decided to take a look in the java String class to check if there was some overloading for the +
operator.
I couldn't find anything, but I know I can do this
String ab = "ab";
String cd = "cd";
String both = ab + cd; //both = "abcd"
Where's that implemented?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
来自 精细手册:
请参见 JLS 中的字符串连接。
From the Fine Manual:
See String Concatenation in the JLS.
编译器将您的代码视为您编写了以下内容:
编辑:有参考吗?好吧,如果我编译和反编译OP的代码,我会得到这个:
所以,就像我说的那样。
The compiler treats your code as if you had written something like:
Edit: Any reference? Well, if I compile and decompile the OP's code, I get this:
So, it's like I said.
这里的大多数答案都是正确的(它是由编译器处理的,+被转换为.append()...)
我想补充一点,每个人都应该在某个时候看看String和append的源代码,它很漂亮感人的。
我相信这可以归结为:
=
但随后一些神奇的事情发生了。这变成:
而大多数人认为它将创建一个包含“ab”的 2 字符数组,然后当它创建一个包含“abc”的三字符数组时将其丢弃。它实际上知道它正在被链接,并在您假设这些是简单的库调用时进行一些超出您假设的操作。
还有一个技巧,如果您有字符串“abc”并且您要求一个结果为“bc”的子字符串,那么它们可以共享完全相同的底层数组。你会注意到有一个开始位置、结束位置和“共享”标志。事实上,如果不是共享的,它有可能扩展字符串数组的长度并在when中复制新字符追加。
现在我只是感到困惑。阅读源代码——这相当酷。
非常晚的编辑:
关于共享底层数组的部分不再那么正确了。他们必须对字符串进行一些去优化,因为人们正在下载巨大的字符串,获取一个很小的子字符串并保留它。这将整个底层数组保存在存储中,在删除所有子引用之前无法对其进行 GC。
Most of the answers here are correct (it's handled by the compiler, + is converted to .append()...)
I wanted to add that everyone should take a look at the source code for String and append at some point, it's pretty impressive.
I believe it came down to something like:
=
But then some magic happens. This turns into:
Whereas most people believe that it will create a 2 character array with "ab", and then throw it away when it creates a three character array with "abc". It actually understands that it's being chained and does some manipulation outside what you would assume if these were simple library calls.
There is also a trick where if you have the string "abc" and you ask for a substring that turns out to be "bc", they CAN share the exact same underlying array. You'll notice that there is a start position, end position and "shared" flag.In fact, if it's not shared, it's possible for it to extend the length of a string array and copy the new characters in when appending.
Now I'm just being confusing. Read the source code--it's fairly cool.
Very Late Edit:
The part about sharing the underlying array isn't quite true any more. They had to de-optimize String a little because people were downloading giant strings, taking a tiny sub-string and keeping it. This was holding the entire underlying array in storage, it couldn't be GC'd until all sub-references were dropped.
它由编译器处理。
It is handled by the compiler.
这是语言规范。
This is special behavior documented in the language specification.
这是在语言层面完成的。 Java 语言规范 对于什么字符串非常具体添加必须做。
It's done at the language level. The Java Language Specification is very specific about what string addition must do.
String
在编译器级别被定义为标准类型,就像 int、double、float 等一样。本质上,所有编译器都有运算符重载。运算符重载不是为开发人员定义的(与 C++ 不同)。有趣的是:这个问题被记录为错误: https://bugs.java.com /bugdatabase/view_bug?bug_id=4905919
String
is defined as a standard type just like int, double, float, etc. on compiler level. Essentially, all compilers have operator overloading. Operator overloading is not defined for Developers (unlike in C++).Interestingly enough: This question was logged as a bug: https://bugs.java.com/bugdatabase/view_bug?bug_id=4905919