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 Look at the following simple expressions in Java The compiler is built internally in 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): This reference value is then converted to string by string conversion. Now only the reference values need to be considered: 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: 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 In String.java code. Does this concept violate object orientation?
stringbuilder in
"x =" + x; and
.append (int) to add an integer string.
public static zero main (string [] args) {string cip = "cip"; String ciop = "ciop"; String plus = cip + ciop; String Build = New StringBuilder (CIP) .append (ciop) .toString (); }
cip + ciop;
New string builder (cip) .append (ciop) .toString (); In other words, the operator
+ string syntax effectively has a shorthand for more verbose
StringBuilder idiom.
Comments
Post a Comment