The "+" operator provided by JAVA, such as Iteger+String, from the perspective of C++, I always want to find out how JAVA overloads this "+" operator, so I looked in the String class, but there is no clue. Discovered, so I wonder how JAVA does it? Let's step by step analyze how JAVA implements "+ operator overloading".
Example
public class Example { public static void main(String[] args) { Integer a = null; String b = a + "456"; System.out.println(b); } }
This program is very simple, it is a "+" operation expression of Integer and String. Running result: null456
Decompile the sample program
Command:
javap -c Example
The decompiled result is as follows:
Compiled from "Example.java" public class com.boyu.budmw.test.Example extends java.lang.Object{ public com.boyu.budmw.test.Example(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: aconst_null 1: astore_1 2: new #2; //class java/lang/StringBuilder 5: dup 6: invokespecial #3; //Method java/lang/StringBuilder."<init>":()V 9: aload_1 10: invokevirtual #4; //Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder; 13: ldc #5; //String 456 15: invokevirtual #6; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 18: invokevirtual #7; //Method java/lang/StringBuilder.toString:()Ljava/lang/String; 21: astore_2 22: getstatic #8; //Field java/lang/System.out:Ljava/io/PrintStream; 25: aload_2 26: invokevirtual #9; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 29: return }
Let’s analyze the main function part:
0: Push the constant null into the operand stack
1 : Pop null from the operand stack and save it into local variable a with index 1
2: new a StringBuilder
5: Copy the space previously created by new and push it into the operand stack
6: Call in progress Initialization
9: Save the result to the operand stack
10: Call StringBuilder.append(java/lang/Object)
13: Push "456" to the top of the stack
15: StringBuilder.append(java/lang /String)
18: Execute toString function
From the above analysis, we can see that it finally generates a StringBuilder object first, and the subsequent "+" operator calls StringBuilder.append() for "+" of. This can explain why the above example program is null456 after running. When appending object,
public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toString(); }
is called to convert the object into String.
Why does JAVA not support operator overloading?
Like in C++, classes overload operators. Personally, I think it will be a difficult problem for operation and maintenance, because there is no standard to restrict operator overloading. Everyone can take it for granted that overloading will cause semantic differences. Large, readability is seriously reduced, so the feature of removing operator overloading in Java is very consistent with its advanced object-oriented nature. So, don’t dwell on this issue.
Postscript
These are some things that are often used during the development process, but they may not be dug so deeply during the normal development process, so they are taken for granted. Later, you can try to continuously explore these small cases that are not discovered.