The string is added without double quotes

I am new to java. I am having doubts about working +=

on String. Why could I use the operator +=

to add String objects without double quotes?

For example, I don't see a compiler error in this piece of code

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

      

When I really thought I needed to use s1+="42"

;

+3


source to share


3 answers


 String s1+=def;

      

This string is valid and then def

is another Java string. Since it compiles successfully, some where earlier in your code you

 String def ="someDeclaredStringBefore";

**Update:**

      

To get clear on what's going on there, let's first see how +

Strings works.

It uses the StringBuilder add method. For ex



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

      

The complete story I wrote here recently:

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

When you wrote String r += 42;

Since you are trying to add a value int

. The corresponding append(int i)

method calls StringBuilder and generates the final string.

+2


source


Using this code to simulate your question above:

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

      

As a result of this byte, Note:

 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

      

Look at the number 16 and 19 , you can clearly see that it calls the StringBuilder class and calls append method

internally in += operator

, which added 42 and in line 19

, and then converted it to String.



EDIT:

the above code actually says so: s = s + 42

so every time you use the plus operator in Integer for String it calls its wrapper class and calls the toString method

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.

      

+2


source


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

      

It worked because 42 is that int

.

The plus sign +

is an overloaded operator in Java. It can either add numbers or add strings.

  • If you use +

    for both numbers it will add it.

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

  • If you use it on both lines, it will add lines.

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

  • If you use it in a combination of numbers and strings, it will just add it together.

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

For more information , check out this Oracle article

+1


source







All Articles