在加号(+)运算符和String.concat()方法给出了相同的结果.
加(+)运算符 ;
str1 + str2;
Run Code Online (Sandbox Code Playgroud)
String concat()方法 ;
str1.concat(str2);
Run Code Online (Sandbox Code Playgroud)
另外,它写在w3schools ;
但是使用JavaScript,方法和属性也可用于原始值,因为JavaScript在执行方法和属性时将原始值视为对象.
那么哪种方式更好地用于组合我们用于基元或JS中的String对象,如果有的话,它们之间的性能优势和劣势是什么?
var firstName = "John" // String
var y = new String("John"); // with String Object
Run Code Online (Sandbox Code Playgroud)
以下是给出相同结果的示例代码;
function example1 () {
var str1 = "Hello ";
var str2 = "World!";
document.getElementById("demo1").innerHTML += str1 + str2;
}
function example2 () {
var str1 = "Hello ";
var str2 = "World!";
document.getElementById("demo2").innerHTML += str1.concat(str2);
}
function example3 () {
var str1 = String("Hello "); …Run Code Online (Sandbox Code Playgroud)我一直在阅读我的团队中其他开发人员生成的代码,他们似乎更喜欢使用+=String连接,而我更喜欢使用,.concat()因为它感觉更容易阅读.
我正在尝试准备一个关于为什么使用.concat()更好的论点,我想知道,两者之间的效率有什么不同吗?
我们应该选择哪个选项?
public class Stuff {
public static void main(String[] args) {
String hello = "hello ";
hello += "world";
System.out.println(hello);
String helloConcat = "hello ".concat("world");
System.out.println(helloConcat);
}
}
Run Code Online (Sandbox Code Playgroud) 我制作的一些代码比较了连接字符串所需的时间"string" + "string":
for(int i = 0; i < 100000000L; i++)
{
String str2 = str + str;
}
Run Code Online (Sandbox Code Playgroud)
到"string".concat("string"):
for(int i = 0; i < 100000000L; i++)
{
String str2 = str.concat(str);
}
Run Code Online (Sandbox Code Playgroud)
哪里str == "string".
我得到的输出与此类似,尽管平均差异通常接近61纳秒:
String str2 = str + str:118.57349468纳秒
String str2 = str.concat(str):52.36809985纳秒
.concat比+66.20539483纳秒更快
这表明,即使循环和赋值给新字符串,也要.concat快于+两倍以上.当我使用更长的字符串(str == "this is a really really very long string that is very long")时,它的速度提高了大约三倍.这特别奇怪,因为如果 …
基本上,我想知道哪种方法更好,
for(int i = 0; i < 10000; i++){
System.out.print("blah");
}
System.out.println("");
Run Code Online (Sandbox Code Playgroud)
要么
String over_9000_blahs = "";
for(int i = 0; i < 10000; i++){
over_9000_blahs += "blah";
}
System.out.println(over_9000_blahs);
Run Code Online (Sandbox Code Playgroud)
还是有更好的方法,我不知道?
可能重复:
java字符串连接
如何提高这块代码的性能:
public static String concatStrings(Vector strings) {
String returnValue = "";
Iterator iter = strings.iterator();
while( iter.hasNext() ) {
returnValue += (String)iter.next();
}
return returnValue;
}
Run Code Online (Sandbox Code Playgroud) 这是一段代码
@Test
public void test_mono_void_mono_empty() {
Mono.just("DATA")
.flatMap(s -> Mono.just(s.concat("-")
.concat(s))
.doOnNext(System.out::println)
.then())
.switchIfEmpty(Mono.just("EMPTY")
.doOnNext(System.out::println)
.then())
.block();
}
Run Code Online (Sandbox Code Playgroud)
这将向控制台提供以下结果:
DATA-DATA
EMPTY
Run Code Online (Sandbox Code Playgroud)
这意味着第一个链flatMap被识别为空链。
另一方面,Reactor 具有以下由方法返回的MonoEmpty类。最重要的是,该方法说明如下:Mono.empty()
/**
* Create a {@link Mono} that completes without emitting any item.
*
* <p>
* <img class="marble" src="doc-files/marbles/empty.svg" alt="">
* <p>
* @param <T> the reified {@link Subscriber} type
*
* @return a completed {@link Mono}
*/
public static <T> Mono<T> empty() {
return MonoEmpty.instance();
}
Run Code Online (Sandbox Code Playgroud)
没有发出任何项目- …
这篇文章说a += b的相当于
a = new StringBuilder()
.append(a)
.append(b)
.toString();
Run Code Online (Sandbox Code Playgroud)
假设我有这段代码:
public class MultiThreadingClass extends SomeThirdPartyClassThatExtendsObject{
public void beginmt(String st) throws IOException {
//st is a thread number
st = new File("c:\\somepath").getCanonicalPath()+"\\"+st;
System.out.println(st);
}
}
Run Code Online (Sandbox Code Playgroud)
假设beginmt在MultiThreading类的单个实例上同时运行多次(线程号为1到15500).可能有这样的情况,它可以打印以下,即一些线程数丢失,一些数字加倍?
c:\somepath\2
c:\somepath\1
c:\somepath\1
c:\somepath\4
c:\somepath\5
c:\somepath\6
c:\somepath\7
c:\somepath\8
c:\somepath\8
c:\somepath\10
...
Run Code Online (Sandbox Code Playgroud)
编辑:
是否安全地说+运算符不会进入某些不安全的发布问题?我想StringBuilder的可以优化到的东西,就像一个实例变量在这种情况下,它可以被不安全公布.
编辑2:
就JLS,上述帖子和上述代码的类似类文件进行检查,要使用的StringBuilders似乎必须包含在不同的堆栈帧中.但是,我仍然想检查某种形式的激进优化是否会导致StringBuilders以某种方式被集中式StringBuilder替换.这听起来可能像听起来合乎逻辑的,当它预测的对象是在非固定的方式只是实施的时候,其实这样的对象可能是不断优化优化.
找到了stringopts.cpp,但还没有找到时间来完全检查它.我希望找到涉及此源文件详细信息的答案.
编辑3:
我仍在寻找包含有关可变对象的积极内联代码的答案.
我在java中使用过String,StringBuilder和StringBuffer.
我想到了这个问题,而我从效率的角度思考.
字符串连接中的"+"是否会影响效率?
可能重复:
java字符串连接
消息来源告诉我们concat实施如下:
public String concat(String str) {
int otherLen = str.length();
if (otherLen == 0) {
return this;
}
int len = value.length;
char buf[] = Arrays.copyOf(value, len + otherLen);
str.getChars(buf, len);
return new String(buf, true);
}
Run Code Online (Sandbox Code Playgroud)
+在字符串方面,实现是否有所不同?怎么样?有没有之间的性能差异+和concat.何时应该选择另一个?
刚刚开始学习java,似乎无法弄清楚这一点.我正在学习learnjavaonline.org上的教程,它教你一些事情,然后要求你编写一个代码来做一个特定的事情,然后检查输出以查看它是否正确.问题是,如果它不正确,它没有说明原因,或者给你一个正确代码的例子.
它希望我使用所有基元输出一个字符串"H3110 w0r1d 2.0 true"
我想出了这个
public class Main {
public static void main(String[] args) {
char h = 'H';
byte three = 3;
short one = 1;
boolean t = true;
double ten = 10;
float two = (float) 2.0;
long won = 1;
int zero = 0;
String output = h + three + one + ten + " " + "w" + zero + "r" + won + "d " + two + " " + t; …Run Code Online (Sandbox Code Playgroud) java ×9
string ×6
performance ×4
concat ×1
concurrency ×1
integer ×1
javascript ×1
jvm ×1
optimization ×1
output ×1
reactor ×1
stringbuffer ×1