parameter references to Java functions, bringing back function results by passing in parameters __ block chain

Source: Internet
Author: User

Java has no pointers, and in cases where the parameters need to be brought back directly to the result, there are some differences compared to the C language, any type in C can be passed by the pointer, in Java, you need to customize a class (the standard type cannot bring back parameters), and this custom class can return the result through the parameters of the function. Achieves a similar effect as a C pointer.

public class Testparameter
{public

  testparameter ()
  {
  } public

  static void Main (string[] args)
  {
    //function incoming standard type parameter, test reference
    //-------------------------------------------
    Integer a = 0;
    Integer b = 0;
    References cannot be
    getValue (A, b);
    No expected results
    System.out.println (a);
    System.out.println (b);

    function to pass in the custom class parameter, test reference
    //-------------------------------------------
    myvalue value = new MyValue ();
    Value.a = 0;
    value.b = 0;
    Reference valid
    GetValue (value);
    Get the desired result
    System.out.println (VALUE.A);
    System.out.println (value.b);
  }

  The parameter is a custom class
  private static void GetValue (MyValue v)
  {
    V.A =;
    V.B = +;
  }

  parameter is a standard type
  private static void GetValue (integer A, integer b)
  {
    a =;
    b = Panax Notoginseng
  ;
}

Custom class
myvalue
{
  //variable initialization public
  int a = 0;
  public int b = 0;
}


The parameters of GetValue (myvalue) are assigned to the function, and the variables in the class are changed as the result is still brought out by the class.

The parameters of GetValue (integer, integer) are changed within the function, but when the function returns, the incoming arguments are not changed, and the second String type does not bring the result back through the parameter, only the object created by new.

String, you can use string[to pass as a parameter, or you can bring back the result of the run inside the function

  public static void Main (string[] args)
  {
    try
    {
      string[] str = new STRING[1];
      Str[0] = "";
      Test2 (str);
      The result of the display of the Test2 is shown
      System.out.print (str[0]);
    }
    catch (Exception e)
    {
      e.printstacktrace ();
    }
  }

  private static void Test2 (string[] str) throws Exception
  {
    str[0] = "Hello world.";
  }


blog.csdn.net/joyous/article/details/9372279
Q Group discussion: 236201801

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.