字符串连接成StringBuilder java [英] String concatenation into StringBuilder java

查看:99
本文介绍了字符串连接成StringBuilder java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个遗留Java文件,它使用字符串连接来构建巨大的String对象。这是一个严重的性能问题。有一个方法就是这样做了以下

I have a legacy Java file which uses String concatenation to build huge String objects.Its a serious performance issue.Is there a method as such which does the following

String test="I am a very bad programmer"
+"to use concatenation"
+"Instead of StringBuilder"
+" or StringBuffer";

StringBuilder strBuilder= new StringBuilder();
strBuilder.append("I am a bad programmer");
strBuilder.append("to use concatenation");
strBuilder.append("Instead of StringBuilder");
strBuilder.append(" or StringBuffer");
String str= strBuilder.toString();

基本上我需要java中的存根只是为了将String实例化作为输入并转换为StringBuilder。过去有人试过这个吗?

basically I need a stub in java just to give a the String instantiation as input and convert into StringBuilder.Anybody tried this in the past?

推荐答案

像你的例子中的固定文字比使用StringBuilder更有效。

A fixed literal like in your example is more efficient than using a StringBuilder.

编译器将检测到固定文字,并将内联为单个值,因此两行

The fixed literal will be detected by the compiler and will be inlined as a single value, so the two lines

String s = "one" + "two" + "three";

String s = "onetwothree";

将生成完全相同的字节码。

will generate exactly the same bytecode.

虽然连接不是用文字进行连接而是用函数调用,但是图片是不同的。

The picture is different though if the concatenation is not done with literals but with function calls.

当你需要动态追加字符串时,更喜欢StringBuilder而不是StringBuffer,因为它略有更快,因为它不同步。

When you need to append strings dynamically, prefer StringBuilder over StringBuffer because it is slightly faster as it is not synchronized.

下面是示例字节码:


public class Test
{
   private String s = "one" + "two" + "three";
}

public class Test2
{
   private String s2 = "onetwothree";
}

这些类的生成字节码是:

the generated bytecode for these classes is:


c:\Temp>javap -c Test
Compiled from "Test.java"
public class Test extends java.lang.Object{
public Test();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."":()V
   4:   aload_0
   5:   ldc     #2; //String onetwothree
   7:   putfield        #3; //Field s:Ljava/lang/String;
   10:  return
}
c:\Temp>javap -c Test2
Compiled from "Test2.java"
public class Test2 extends java.lang.Object{
public Test2();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."":()V
   4:   aload_0
   5:   ldc     #2; //String onetwothree
   7:   putfield        #3; //Field s:Ljava/lang/String;
   10:  return
}

正如您所看到的,两个变量的处理方式相同。

As you can see both variables are treated the same way.

我不认为这属于语言规范,因为这只是编译器优化。

I don't think this belongs to the language specification, as this is "just" a compiler optimization.

一个不同的编译器(我使用的是Sun编译器)可能会做一些完全不同的事情 - 只要行为没有改变就行了。

A different compiler (I was using the Sun compiler) might be doing something completely different - which is OK as long as the behaviour doesn't change.

这篇关于字符串连接成StringBuilder java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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