The difference between the value of a Java function parameter and the pass-through reference.

Source: Internet
Author: User
Tags set set

(a) Basic data type: A value is passed, and the method does not change the value of the argument.

[Java]View PlainCopy
    1. Public class Testfun {
    2. Public static void Testint (int i) {
    3. i=5;
    4. }
    5. Public static void Main (string[] args) {
    6. int a=0;
    7. Testfun.testint (a);
    8. System.out.println ("a=" +a);
    9. }
    10. }

Program execution Result: a=0.

  (b) object type parameter: A reference, the method body changes the parameter reference, does not change the reference of the argument, but it is possible to change the property value of the argument object.

Give two examples:

(1) The method body changes the parameter reference, but does not change the argument reference, the actual parameter value is not changed.

[Java]View PlainCopy
    1. public class  testfun2 {  
    2. public  Static void teststr (string str) {  
    3. str= "Hello";
    4. }  
    5. public static void main ( String[] args)  {  
    6. string s= "1"  ;  
    7. testfun2.teststr (s);   
    8. System.out.println ( Span class= "string", "s=" +s),  //argument s reference unchanged, value is not changed   
    9. }  
    10. }  

Execution result Print: S=1

(2) In vivo, the content of the actual parameter object is changed by reference, which is "content", reference or unchanged.

[Java]View PlainCopy
  1. import Java.util.HashMap;
  2. import Java.util.Iterator;
  3. import Java.util.Map;
  4. import Java.util.Set;
  5. public class Test {
  6. Public void A (Map a,string b)
  7. {
  8. A.put ("test", b);
  9. }
  10. Public void B ()
  11. {
  12. Map a = new HashMap ();
  13. A.put ("test", "a");
  14. String B = "B";
  15. Printmap (a);
  16. A (a, b);
  17. Printmap (a);
  18. }
  19. Public void Printmap (Map a)
  20. {
  21. Set set = A.entryset ();
  22. Iterator it = Set.iterator ();
  23. while (It.hasnext ())
  24. {
  25. Map.entry e = (map.entry) it.next ();
  26. System.out.println ("Key:" +e.getkey () +"Value:" +e.getvalue ());
  27. }
  28. }
  29. Public static void Main (String avgs[])
  30. {
  31. Test T = new test ();
  32. T.B ();
  33. }
  34. }


Execution result, print: Key:test value:b. It is visible that the contents of the argument are changed within method A ().

(3) The second example is taking map for example, and often involves stringbuffer:

[Java]View PlainCopy
  1. Public class TestFun4 {
  2. Public static void Teststringbuffer (StringBuffer sb) {
  3. Sb.append ("Java"); Changed the contents of an argument
  4. }
  5. Public static void Main (string[] args) {
  6. StringBuffer sb= New StringBuffer ("my");
  7. New TestFun4 (). Teststringbuffer (SB);
  8. System.out.println ("sb=" +sb.tostring ()); The content has changed.
  9. }
  10. }


Execution result, print: Sb=my java.

So the comparison parameter is two examples of string and stringbuffer to understand what "changes the contents of an Argument object".

  Summarize:

First: The Java method basic data type is a pass value, the object type is quoted, this is true.

Second: When a parameter is an object, no reference to the argument object is changed, regardless of the action in the method body.

Third: When a parameter is an object, the contents of the argument object are changed only if the object's contents are changed inside the method.

(RPM) The difference between the value of a Java function parameter and the pass-through reference.

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.