是否“+”在字符串连接中使用会影响效率吗? [英] Does "+" use in String concatenation affect efficiency?

查看:151
本文介绍了是否“+”在字符串连接中使用会影响效率吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在java中使用过String,StringBuilder和StringBuffer。

我想到了这个问题,而我从效率的角度思考。

I have worked with String, StringBuilder and StringBuffer in java.
I thought of this question, while I was thinking from efficiency point of view.

字符串连接中的+是否会影响效率?

推荐答案

是的,但是在大多数情况下这都不重要。

Yes, but so little it shouldn't matter most of the time.

对字符串常量使用'+'是最有效的编译器可以执行连接。

Using '+' for string constants is the most efficient as the compiler can perform the concatenation.

如果要加入两个字符串, concat 方法效率最高它避免使用StringBuilder。

If you are joining two Strings, the concat method is the most efficient as it avoids using a StringBuilder.

除了向后兼容性之外,几乎没有充分的理由使用StringBuffer。 StringBuilder或StringWriter是更好的选择。但是,它仍然比JDK中的StringBuilder更频繁地使用:P

There is almost never a good reason to use StringBuffer except for backward compatibility. StringBuilder or StringWriter are a better choice. However, it is still used explicitly more often than StringBuilder in the JDK :P

StringBuffer已经死了,StringBuffer也很长

这篇关于是否“+”在字符串连接中使用会影响效率吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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