【问题标题】:String append without double-quotes不带双引号的字符串追加
【发布时间】:2014-10-27 12:23:54
【问题描述】:

我是java初学者。我对 String 上的 += 操作有疑问。为什么我可以使用+= 运算符来附加没有双引号的字符串对象?

例如,我在这段代码上没有编译器错误

String s1 = "abc";
String s1+=42;

当我真的认为我必须使用s1+="42"时;

【问题讨论】:

标签: java string append operator-keyword double-quotes


【解决方案1】:
 String s1+=def;

那一行是有效的,然后def 是另一个 Java 字符串。由于它编译成功,在你的代码之前的一些地方,你有

 String def ="someDeclaredStringBefore";

**Update:**

要弄清楚那里发生了什么,首先让我们看看+ 如何处理字符串。

它使用 StringBuilder 追加方法。例如

 StringBuilder compilerGeneratedBuilder = new StringBuilder();  
 compilerGeneratedBuilder.append("str");  
 compilerGeneratedBuilder.append("ingcon");  
 compilerGeneratedBuilder.append("catenation");  
 String finalString = compilerGeneratedBuilder.toString();

我最近在这里写的全文:

http://codeinventions.blogspot.com/2014/08/compiler-version-string-concatenation.html

当你写String r += 42;

因为您尝试添加 int 值。对应的append(int i)方法调用StringBuilder并生成最终字符串。

【讨论】:

  • 不一定是String。哦,他们刚刚更新了他们的问题。
  • @SotiriosDelimanolis 感谢您的关注。我刚刚写完并退出了这个页面。不知道编辑。我也编辑了帖子。
【解决方案2】:

使用此代码模拟您上面的问题:

    String s = "asd";
    s+=42;
    System.out.println(s);

会产生这个byteCote:

 Code:
      0: ldc           #16                 // String asd
      2: astore_1
      3: new           #18                 // class java/lang/StringBuilder
      6: dup
      7: aload_1
      8: invokestatic  #20                 // Method java/lang/String.valueOf:(
java/lang/Object;)Ljava/lang/String;
     11: invokespecial #26                 // Method java/lang/StringBuilder."<
nit>":(Ljava/lang/String;)V
     14: bipush        42
     16: invokevirtual #29                 // Method java/lang/StringBuilder.ap
end:(I)Ljava/lang/StringBuilder;
     19: invokevirtual #33                 // Method java/lang/StringBuilder.to
tring:()Ljava/lang/String;
     22: astore_1
     23: getstatic     #37                 // Field java/lang/System.out:Ljava/
o/PrintStream;
     26: aload_1
     27: invokevirtual #43                 // Method java/io/PrintStream.printl
:(Ljava/lang/String;)V
     30: return

查看数字 16 和 19,您可以清楚地看到它调用 StringBuilder 类并在内部调用 append method 到附加 42 的 += operatorline 19 然后将其转换为字符串。

编辑:

上面的代码实际上是这样写的:s = s + 42,所以每次你使用加号操作符到一个 Integer 到 String 时,它都会调用它的包装类并调用 toString 方法

JLS

Any type may be converted to type String by string conversion.

A value x of primitive type T is first converted to a reference value as if by giving it as an argument to an appropriate class instance creation expression (§15.9):

If T is boolean, then use new Boolean(x).

If T is char, then use new Character(x).

If T is byte, short, or int, then use new Integer(x).

If T is long, then use new Long(x).

If T is float, then use new Float(x).

If T is double, then use new Double(x).

This reference value is then converted to type String by string conversion.
Now only reference values need to be considered:

If the reference is null, it is converted to the string "null" (four ASCII characters n, u, l, l).

Otherwise, the conversion is performed as if by an invocation of the toString method of the referenced object with no arguments; but if the result of invoking the toString method is null, then the string "null" is used instead.

【讨论】:

  • 找到 JLS 的相关部分并从中引用不是更好吗?查看编译器输出很粗糙......如果这是编译器错误怎么办?
  • @nem 如果您使用的是 Windows,请转到命令提示符和 cd to_folder_of_your_project/bin,然后使用 javap -c NAME_OF_CLASS
【解决方案3】:
String s1 = "abc";
String s1+=42;

这很有效,因为 42 是 int

加号+ 是Java 中的重载 运算符。它可以添加数字或附加字符串。

  1. 如果您在两个号码上都使用+,它将添加它。

    System.out.println(1+1); //2

  2. 如果您在两个字符串上都使用它,它将附加字符串。

    System.out.println("abc"+"def"); //abcdef

  3. 如果你在数字和字符串的组合中使用它,它只会将它附加在一起。

    System.out.println(1+"1"); //11

欲了解更多信息check out this article on Oracle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多