"Java Fundamentals" 11, Java methods only value is passed, no reference is passed

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/linkstar/p/5951141.html
public class Example {
String teststring = new String ("good");
Char[] Testchararray = {' A ', ' B ', ' C '};

public static void Main (string[] args) {
Example ex = new Example ();
Ex.change (Ex.teststring,ex.testchararray);
System.out.println (ex.teststring);
System.out.println (Ex.testchararray);
}

public void Change (String teststring,char[] testchararray) {
teststring = "Hhhhhh";
Testchararray[0] = ' W ';
}
}
What is the last output of this code?
Give your answer after consideration.
+ View Code
Goodwbc

The first thing to understand is what is the Java approach passed on?

For a base type, the value of the base type is passed, and the address is passed for the reference type.

So whatever it is, the value is passed, because you can interpret the address as 9x0000, which is also a value.

So all that is passed is the value.

When does the method change its original value, and when does it not?

1, as long as the basic type, pass the value, this value is copied a copy out, so how will not change.

2, the reference type, passes the address, if this address has changed, then the original value must be unchanged.

3, the reference type, passes the address, if the address does not change, and changes the address corresponding to the object's properties, so long will change the original value.

If you do not understand, then simply, as long as the incoming object in the method to re-assign a new object, then the original is not become.

As in the method string, re-assigned a string, this time the address and changed, so the original unchanged.

The char array, instead of re-copying a new char array, changes the elements in the char array, which changes the original value.

For the memory analysis, the following blog is particularly clear, I will not say more.

It's really important to understand this.

Http://www.cnblogs.com/wangzhongqiu/p/6602934.html

"Java Fundamentals" 11, Java methods only value is passed, no reference is passed

Related Article

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.