java - How does the String class override the + operator? -
why in java you're able add strings + operator, when string class? in thestring.java
code did not find implementation operator. concept violate object orientation?
let's @ following simple expressions in java
int x=15; string temp="x = "+x;
the compiler converts "x = "+x;
stringbuilder
internally , uses .append(int)
"add" integer string.
any type may converted type string string conversion.
a value x of primitive type t first converted reference value if giving argument appropriate class instance creation expression (§15.9):
- if t boolean, use new boolean(x).
- if t char, use new character(x).
- if t byte, short, or int, use new integer(x).
- if t long, use new long(x).
- if t float, use new float(x).
- if t double, use new double(x).
this reference value converted type string string conversion.
now reference values need considered:
- if reference null, converted string "null" (four ascii characters n, u, l, l).
- otherwise, conversion performed if invocation of tostring method of referenced object no arguments; if result of invoking tostring method null, string "null" used instead.
the tostring method defined primordial class object (§4.3.2). many classes override it, notably boolean, character, integer, long, float, double, , string.
see §5.4 details of string conversion context.
optimization of string concatenation : implementation may choose perform conversion , concatenation in 1 step avoid creating , discarding intermediate string object. increase performance of repeated string concatenation, java compiler may use stringbuffer class or similar technique reduce number of intermediate string objects created evaluation of expression.
for primitive types, implementation may optimize away creation of wrapper object converting directly primitive type string.
the optimized version not full wrapped string conversion first.
this illustration of optimized version used compiler, albeit without conversion of primitive, can see compiler changing things stringbuilder in background:
http://caprazzi.net/posts/java-bytecode-string-concatenation-and-stringbuilder/
this java code:
public static void main(string[] args) { string cip = "cip"; string ciop = "ciop"; string plus = cip + ciop; string build = new stringbuilder(cip).append(ciop).tostring(); }
generates - see how 2 concatenation styles lead same bytecode:
l0 linenumber 23 l0 ldc "cip" astore 1 l1 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.<init>(ljava/lang/string;)v aload 2 invokevirtual java/lang/stringbuilder.append(ljava/lang/string;)ljava/lang/stringbuilder; invokevirtual java/lang/stringbuilder.tostring()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.<init>(ljava/lang/string;)v aload 2 invokevirtual java/lang/stringbuilder.append(ljava/lang/string;)ljava/lang/stringbuilder; invokevirtual java/lang/stringbuilder.tostring()ljava/lang/string; astore 4 l4 linenumber 27 l4 return
looking @ example above , how byte code based on source code in given example generated, able notice compiler has internally transformed following statement
cip+ciop;
into
new stringbuilder(cip).append(ciop).tostring();
in other words, operator +
in string concatenation shorthand more verbose stringbuilder
idiom.
Comments
Post a Comment