Whether parameter passing in Java is passed by reference or by value

Source: Internet
Author: User
Tags call by reference java reference

Recently went to interview, an interviewer asked about the parameters in the Java problem, I feel that this piece is not deep enough to understand. Today we will learn about interfaces and abstract classes in Java. The following is the directory outline for this article:

One. What is passed by value and what is passed by reference

Two. Whether parameter passing in Java is passed by value or by reference

Three Summarize

If there are any shortcomings, please understand and welcome the criticism, not very grateful.

One. What is passed by value and what is passed by reference

called by value: during parameter passing, formal parameters and arguments occupy two completely different memory spaces. The content stored by the formal parameter is a copy of the contents of the argument store .

Passing by value is a good understanding of data passing in basic data types, and the test code is as follows:

 Public class Test {    publicstaticvoid  main (string[] args) {        int age = 0;        System.out.println ("Before change age=" + age);        Change (age);        System.out.println ("After change age=" + age);    }          Public Static void Change (int  a) {        a=20;    }}

Operation Result:

Before Change age=0
After change age=0

This result is believed to have been guessed by many friends: because when you call change in the main method, the parameter that is actually passed to the changes method is just a copy of age, so modifying the copy does not change the value of the original.

Call by reference: During parameter passing, the parameters and arguments are exactly the same chunk of memory space, both equals.

For example:

int a=0; Func (&a); printf ("%d\n", a);} Func (int **b=;}

Cough, this seems to be the C language by reference to pass. The Java reference pass cannot be written.

Two. Whether parameter passing in Java is passed by value or by reference

Before we jump to conclusions, let's look at an example:

 Public classPassbyvalue {PrivateString name;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; } @Override PublicString toString () {return"Passbyvalue [name=" + name + "]"; }     Public Static voidMain (string[] args) {passbyvalue mydear=NewPassbyvalue (); Mydear.setname ("Dontknow"); String Mylove= "Dontknow"; intHerage=0; System.out.println ("Mydear name=" +mydear.getname ()); System.out.println ("Mylove =" +mylove);        System.out.println (Herage);        Stringchange (Mylove,mydear,herage); System.out.println ("============================================================"); System.out.println ("Mydear name=" +mydear.getname ()) System.out.println (Mylove);
System.out.println (Herage)
      } Public Static voidStringchange (String A,passbyvalue B,intAge ) {a= "Gaoshan";//This sentence is equivalent to a=new String ("Gaoshan")B.setname ("Gaoshan");
age=20; }}

The results of the operation are as follows :

Mydear Name=dontknow
Mylove =dontknow
0
============================================================
Mydear Name=gaoshan
Mylove =dontknow
0

We found that the name attribute in Mydear was changed by the Stringchange () method, and the value of Mylove did not change.

Here's a question: Mydear and Mylove are reference data types, why Mydear property values change, and Mylove doesn't change?

In fact, the reason has been said in the comments: In the Stringchange method,a= "Gaoshan", this sentence is equivalent to a=new String ("Gaoshan"), in fact a as a data reference has changed, pointing to a new memory address, Mydear, as a data reference, does not change, and still points to the object that Mydear points to in the main method.

Three. Summary

In Java, there is no call by reference, because during parameter passing, a variable that passes the base data type, or a reference to an object, is actually a copy of the pass.

In the above example, the object that Mydear points to is changed because, in the Stringchange method, the object's object is changed by the copy B of the Mydear, which is manipulated by the objects pointed to by Mydear, but

Mydear itself as an object reference has not changed. The mylove does not change because the value of Mylove copy A is changed directly in the Stringchange method, and there is no modification to the object that the Mylove points to, so we see the Main method The object pointed to by Mylove has not changed

This article references

Java parameter passing (Super Classic): http://blog.sina.com.cn/s/blog_4b622a8e0100c1bo.html

Whether parameter passing in Java is passed by reference or by value

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.