java - How does the String class override the + operator? -


Why in Java, you can combine the string with the + operator, when the string is a class? I did not get any implementation for this operator in the String.java code. Does this concept violate object orientation?

Look at the following simple expressions in Java

  int x = 15; String tag = "x =" + x;   

The compiler is built internally in stringbuilder in "x =" + x; and .append (int) to add an integer string.

Any type can be converted into string by string conversion.

The value of primitive type T is first converted into a reference value such as by giving it the argument of the appropriate class instance creation expression (§ 15.9):

  • If T is Boolean, then use the new Boolean (x) if the T is the four, then use the new character (x).
  • If T byte is small or integer, then use a new integer (x).
  • If T is long, then use the new Long (X).
  • If T is float, then use the new float (x).
  • If T is double, then use the new double (x).

    This reference value is then converted to string by string conversion.

    Now only the reference values ​​need to be considered:

    • If the reference is empty, then it is "null" (four ASCII characters n, u, L, L) Is converted to.
    • Otherwise, the conversion is such that a greeting of the toString method of the object referenced with any argument; But if the tap to apply the toString method is empty, then the string "tap" is used instead.

      The toasting method can be found in the original orbit object (§4.3 .2). See a .5.4 for details on the string conversion context. / Strong> An implementation can choose to avoid making and forming a conversion and an assembly in one step, and then repeating an intermediate string object In order to improve the performance of string combinations, a Java compiler can use StringBuffer class or some similar technique of intermediate string objects to represent the origin of an expression You went by evaluation to reduce the number of that.

      For primitive types, an implementation can also be optimized by converting an art object directly into a primitive type from a string.

      The customized version will not actually make a complete string conversion altogether.

      This is a good example of the optimized version used by the compiler, although without the transition of primitive, where you can see the compiler transforming things into a stringbiller in the background:


      This java code:

        public static zero main (string [] args) {string cip = "cip"; String ciop = "ciop"; String plus = cip + ciop; String Build = New StringBuilder (CIP) .append (ciop) .toString (); }   

      Produces this - See how the lead combination for two combination styles: very small byte: 1 LineNumber 24 L1 LDC "ciop" ASTORE 2 // CIP + ciop L2 LineNumber 25 L2 New Java / Lang / StringBuilder DUP aload 1 INVOKESTATIC Java / Lang / String.valueOf (Ljava / Lang / Object;) Ljava / Lang / String; INVOKESPECIAL JAVA / LANG / StringBuilder. & Lt; Init & gt; (Ljava / Lang / String;) V aload 2 INVOKEVIRTUAL Java / Lang / StringBuilder.append (Ljava / Lang / String;) Ljava / Lang / StringBuilder; Invokevertal Java / Long / StringBuilder. Tosting () Ljava / lang / String; ASTORE 3 // New StringBuilder (CIP) .append (ciop) .toString () L3 LineNumber 26 L3 New Java / Lang / StringBuilder DUP Aload 1 INVOKESPECIAL Java / Lang / StringBuilder. & Lt; Init & gt; (Ljava / Lang / String;) VLoad2Invoquirtual Java / Lang / StringBinder.Append (Java / Lang / String;) Java / Lang / StringBuilder; Invokevertal Java / Long / StringBuilder. Tosting () Ljava / lang / String; ASTORE 4 L4 LINQ 27 L4 RETURN

      In view of the above example and how the byte code based on the source code is generated in the given example, you will be able to see that the compiler is internally The following statement has been changed

        cip + ciop;   

      In

        New string builder (cip) .append (ciop) .toString (); In other words, the operator  +  string syntax effectively has a shorthand for more verbose  StringBuilder  idiom.   

Comments