In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, the editor will show you how to use the plus operator in JAVA grammar. The knowledge points in this article are very detailed. Friends who feel helpful can browse the content of the article with the editor, hoping to help more friends who want to solve this problem find the answer to the problem. Follow the editor to learn more about "how to use the plus sign operator in JAVA grammar".
The "+" operator provided by JAVA, such as Iteger+String, from C++ 's point of view, always wanted to find out how JAVA overloaded the "+" operator, so they went into the class String, but did not find any eggs, so they wondered how JAVA did it. Let's take a step-by-step analysis of 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 simply a "+" operation expression of Integer and String. Running result: null456
Reverse compile 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. ": () 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. ": () 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: save null from the Operand stack to the local variable a with index 1
2:new, a StringBuilder.
5: copy the space from the previous new and press it into the Operand stack
6: call for initialization
9: save the result to the Operand stack
10: call StringBuilder.append (java/lang/Object)
13: press "456" into the top of the stack
15:StringBuilder.append (java/lang/String)
18: execute the toString function
From the above analysis, we can see that in the end, the husband becomes a StringBuilder object, and the subsequent "+" operator calls StringBuilder.append () for "+". This can explain why it is null456 after the above sample program is run. It is called when append object is called.
Public static String valueOf (Object obj) {return (obj = = null)? "null": obj.toString ();}
Converted object into String.
Why JAVA does not support operator overloading
For example, if the class in C++ overloads the operator, I think it will be a rare problem in operation and maintenance, because there is no standard to restrict operator overloading, which can be taken for granted, resulting in a big semantic difference and a serious reduction in readability, so removing operator overloading from java is very consistent with its advanced object-oriented.
Thank you for reading, the above is the whole content of "how to use the plus operator in JAVA grammar". Friends who learn it, hurry up and do it. I believe that the editor will certainly bring you better quality articles. Thank you for your support to the website!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.