2014-09-03 99 views
3

我是java的初學者。我對String上的+=操作有疑問。爲什麼我可以使用+=運算符在沒有雙引號的情況下附加String對象?字符串追加沒有雙引號

比如我就在這一段代碼

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

當我其實是想,我必須使用s1+="42"沒有編譯器錯誤;

+0

你有一個名爲'def'範圍變量? – 2014-09-03 05:36:36

+0

有很好的解釋了這個問題 http://stackoverflow.com/questions/47605/string-concatenation-concat-vs-operator – 2014-09-03 05:44:48

回答

2
String s1+=def; 

該行有效,然後def是另一個Java String。由於它成功之前,你的代碼編譯,一些地方,你有

String def ="someDeclaredStringBefore"; 

**Update:** 

要得到什麼happennig那裏,首先讓我們看看如何+作品上字符串的清晰度。

它使用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和最終的字符串生成。

+0

這並不一定非得是'String'。哦,他們剛剛更新了他們的問題。 – 2014-09-03 05:47:37

+0

@SotiriosDelimanolis感謝您的注意。我剛剛寫下並退出了此頁面。不知道編輯。我也編輯了這篇文章。 – 2014-09-03 05:56:31

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內部撥打+= operator(附加42),然後在line 19中將其轉換爲字符串。

編輯:

上面的代碼實際上是在說這樣的:s = s + 42,所以每次使用加法運算的整數的字符串,它會調用它的包裝類,並調用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. 
+0

找到JLS的相關部分並引用它不是更好嗎?看着編譯器的輸出是粗糙的......如果這是一個編譯器錯誤呢? – 2014-09-03 05:52:59

+0

@nem如果你正在使用windows轉到命令提示符和'cd to_folder_of_your_project/bin'並在使用'javap -c NAME_OF_CLASS'後 – 2014-09-03 05:53:16

+0

@StephenC增加/ – 2014-09-03 06:02:10

1
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