Concept of Java value transfer and reference Transfer

Source: Internet
Author: User

Many books have mentioned that only value passing exists in Java, but today we see this in the source code of a NanoHTTPD:

 

1 if (qmi >= 0) {2   decodeParms(uri.substring(qmi + 1), parms);3   uri = decodePercent(uri.substring(0, qmi));4 }
 1 /** 2          * Decodes parameters in percent-encoded URI-format ( e.g. 3          * "name=Jack%20Daniels&pass=Single%20Malt" ) and adds them to given 4          * Map. NOTE: this doesn't support multiple identical keys due to the 5          * simplicity of Map. 6          */ 7         private void decodeParms(String parms, Map<String, String> p) { 8             if (parms == null) { 9                 p.put(QUERY_STRING_PARAMETER, "");10                 return;11             }12 13             p.put(QUERY_STRING_PARAMETER, parms);14             StringTokenizer st = new StringTokenizer(parms, "&");15             while (st.hasMoreTokens()) {16                 String e = st.nextToken();17                 int sep = e.indexOf('=');18                 if (sep >= 0) {19                     p.put(decodePercent(e.substring(0, sep)).trim(),20                             decodePercent(e.substring(sep + 1)));21                 } else {22                     p.put(decodePercent(e).trim(), "");23                 }24             }25         }

 

The code above passes in a Map object when calling the decodeParms method. This method returns the void type. Therefore, the input parameters can only be modified within the method, but cannot be returned, however, this code clearly affects the External Object of the correct method. This is inconsistent with the statement that only values are transmitted in java. Is there a reference transfer in java?

 

Later, I checked the document and found that only value passing exists in java. In java, the parameter passing Method for objects and basic types is quite different. For basic types, java only copies itself, the object is the value of the pointer to the object.

However, the pointer value of the passed object does not mean reference transfer. Let's look at the following code:

1 public class TestPassDemo {2 3 @ Test 4 public void test () {5 TestPassDemo test = new TestPassDemo (); 6 Map <String, String> map1 = new HashMap <String, string> (); 7 map1.put ("username", "David"); 8 Map <String, String> map2 = new HashMap <String, String> (); 9 map2.put ("username", "Nick"); 10 String user = "David"; 11 int I = 1; 12 changeValue (map1, map2, user, I ); 13 14 // If the print is: {username = admin}, it indicates that the java object is passed the address. If it is: {username = David}, it is passed the value of 15 System. out. println (map1); // {username = admin} 16 17 System. out. println (map2); // {username = Nick} 18 // If the print is admin, it indicates that the java String object is transmitted as the address. If it is: david passed the value 19 System. out. println (user); // David 20 System. out. println (I + ""); // 121} 22 23 public void changeValue (Map <String, String> map1, Map <String, String> map2, String str, int I) {24 map2 = map1; 25 map1.put ("username", "Jack"); 26 map2.put ("username", "admin"); 27 str = "admin "; 28 I = 10; 29} 30 31}

The output result is as follows:

{username=admin}{username=Nick}David
1

 

This shows that the object passing in java is only a pointer to the object, and can also be understood as a value passing.

This explains why only the internal value passing method in java mentioned above can change external variables.

The so-called object in java is actually the pointer concept in C language. In essence, it is to open up a space in the heap memory and then use a pointer to reference it. For example:

 

A Person object is an object in java. Person has the name and age attributes. In this case, a pointer is required to reference this Person object, the Person object also contains references to the name object and age object. The so-called java object is actually implemented using the pointer concept in C language.

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.