Value passing and reference passing for Java

Source: Internet
Author: User

Not much to say, see two questions.

In the first question, the following code executes the result:

 Package com.zhyea.test;  Public class Test {    publicstaticvoid  main (string[] args) {        = "123";        Changestring (str);        System.out.println (str);    }      Public Static void changestring (String str) {        = "ABCDEFG";    }}

In the second question, the following code executes the result:

 Package com.zhyea.test;  Public class MyTest {    publicstaticvoid  main (string[] args) {        new person ("Robin");
changepersonname (Robin,"Zhangsan");    SYSTEM.OUT.PRINTLN (Robin); }     Public Static voidchangepersonname (person p, String name) {p.setname (name); }}classPerson {String name; intAge ;  PublicPerson (String name,intAge ) {         This. Name =name;  This. Age =Age ; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     PublicString toString () {return"Person name:[" + name + "], age:[" + Age + "]"; }}

Look at the back of the mind run, later discuss the results.

Value passing: When a method is called, the value passed to the parameter is arguments, and the change in the parameter value when the method is executed does not affect the argument.

Reference delivery: Also known as address delivery. When a method is called, the reference to the actual parameter (the address, not the value of the parameter) is passed to the corresponding formal parameter in the method, and in the execution of the method, the operation of the formal parameter is actually the operation of the actual parameter, and the change of the parameter value in the execution of the method will affect the value of the actual parameter.

The interpretation of reference passing is a bit obscure, isn't it. But it does not affect understanding.

The two variables passed here are reference types (string and person). So the explanation passed by reference, the first output should be "ABCDEFG", the second output is "person Name:[zhangsan", age:[29] ".

Well. It's the same as I thought. But I was wrong.

This question has been discussed in CSDN: http://bbs.csdn.net/topics/390245117.

Direct conclusion: There is no reference passing in Java. The value in Java that the argument gives to the method parameter is a copy of its actual value. The method does not have any effect on the argument for any action on the parameter.

Come back and say those two questions:

The first question result is better understanding, and the conclusion is consistent, does not explain.

The second question also needs to be explained, in this problem, the value of the actual argument does not change, the change is the value of the argument to the object of the property. The explanation is rather clumsy. This involves some relationship between reference types and memory. You can look up some information for yourself.

I understand that if you compare a reference type object to a house, then the variable (argument) of the reference type is a number that opens the house. The argument told the method of the house number, and the method knew that the number could be found and entered the house. But even if the way to empty the House, the house's number is not moving.

Then take an example for the second question to understand:

 Packagecom.zhyea.test; Public classMyTest { Public Static voidMain (string[] args) {person Robin=NewPerson ("Robin", 29);        Changeperson (Robin);    SYSTEM.OUT.PRINTLN (Robin); }         Public Static voidChangeperson (person p) {Person Tom=NewPerson ("Tom", 16); P=Tom; }}classPerson {String name; intAge ;  PublicPerson (String name,intAge ) {         This. Name =name;  This. Age =Age ; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     PublicString toString () {return"Person name:[" + name + "], age:[" + Age + "]"; }}

That's it.

Value passing and reference passing for Java

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.