Some summary of Java object reference passing and value passing

Source: Internet
Author: User

1. When the object is passed as a function parameter, it is passed as a reference to the original object, and changing the value of the parameter object affects the original object. 2. When the object is the return value of a function, passing it is also a reference pass, changing the passed object when it affects the original object 3. When object A assigns a value to another object B (regardless of whether B has been new to open space), B is a reference to the A object, and change B affects a 4. The only way to pass a reference is to copy the object by using the basic data type assignment, such as the last method in the previous example. The object that appears is a new object, not a reference to the original object supplement: 1. The string type in Java is also an object, but when the assignment is passed, the value is passed in the same way as the general data type. String is a special case. Summary: The transfer of objects is basically done by reference passing, and the only way to pass a non-reference is to use the basic data type assignment method (or implement a copy copy function yourself) after you have opened up a new object space.

Attach the test code and results:

        String str = new String ("abc");        char[] ch = {' A ', ' B ', ' C '};        LOG.D ("Levin", str+ "\ n");            LOG.D ("Levin", String.valueof (CH));        Change (str,ch);                System.out.print ("\ n");            LOG.D ("Levin", str+ "\ n");            LOG.D ("Levin", String.valueof (CH));                Person person  = new person ();        Person.name = "Bill";        Person.age = ten;        Changeperson (person);        LOG.D ("Levin", person.name+ "\ n");            LOG.D ("Levin", String.valueof (Person.age));
    public void Changeperson (person person) {        person.name = "Jobs";        Person.age =;      }

06-07 12:33:57.178:d/levin (8091): ABC 06-07 12:33:57.578:d/levin (8091): ABC 06-07 12:33:58.548:d/levin (8091): ABC 06-07 12:33:58.988:d/levin (8091): GBC 06-07 12:34:00.158:d/levin (8091): Jobs 06-07 12:34:00.418:d/levin (8091): 50

Some summary of Java object reference passing and value passing

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.