In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "the definition of parameter transfer type in Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
First of all, look at the definition:
Value passing means that the method receives the value provided by the caller
Reference passing means that the method receives the variable address provided by the caller
In the past, when learning C++, parameter transmission was divided into value transfer and reference transmission. Many domestic java textbooks are willing to understand object transfer as reference transmission. Why do they say so? Take a look at the following example:
Import java.util.Calendar; public class ChangeValue {public static void main (String [] args) {Calendar oc = Calendar.getInstance (); System.out.println ("origin:" + oc.getTime ()); changeDate (oc); System.out.println ("after:" + oc.getTime ());} static void changeDate (Calendar pd) {pd.set (1970, 1,1) }}
Program output at some point:
Origin:Thu Jan 05 21:15:59 CST 2012 after:Sun Feb 01 21:15:59 CST 1970
When the value of the oc object changes, many people think that java object passing is actually reference passing.
The process should go like this:
When you run the changeDate function, the method gets a copy of the object reference, and oc and pd reference the same object at the same time, so after the function ends, pd has disappeared, but the changes to the reference object also affect the same object referenced by oc. Combined with the previous definition understanding, this should be the process of value transfer (passing is a copy of the object reference).
Here is another example, the exchange function of two objects (which can be easily implemented in C++):
Public class Swap {public static void main (String [] args) {ObjectSample o1 = new ObjectSample ("hello"); ObjectSample O2 = new ObjectSample ("Hello"); System.out.println ("before swap o1:" + o1.getTitle () + "O2:" + o2.getTitle ()); Swap.swapObject (o1, O2) System.out.println ("after swap o1:" + o1.getTitle () + "O2:" + o2.getTitle ());} static void swapObject (ObjectSample o1, ObjectSample o2) {ObjectSample temp = new ObjectSample ("temp"); temp = o1; o1 = O2; O2 = temp;}} class ObjectSample {private String title ObjectSample (String title) {this.title = title;} public String getTitle () {return title;}}
Output result:
Before swap o1:hello O2: Hello after swap o1:hello O2: Hello
Java does not exchange the values of two objects in the exchange program, it exchanges copies of two objects, and can not let the object parameters refer to a new object. The reason is that java uses value transfer rather than reference transfer.
This is the end of the definition of parameter passing type in Java. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.