The parameter reference of a Java function is returned by passing in the parameter.

Source: Internet
Author: User

Java has no pointer. When the result needs to be brought back directly by parameters, it is different from the C language.LanguageAny type can be passed through pointers. in Java, a class needs to be customized (standard type cannot bring back parameters), and this custom class can return results through function parameters, similar to the C pointer.

 

Public class testparameter {public testparameter () {} public static void main (string [] ARGs) {// pass in standard type parameters for the function, test reference // --------------------------------------------- integer a = 0; integer B = 0; // The reference cannot be getvalue (a, B); // The expected result is not returned. out. println (a); system. out. println (B); // input the custom class parameter to the function, test reference // ----------------------------------------------- myvalue value = new myvalue (); value. a = 0; value. B = 0; // reference valid getvalue (value); // obtain the expected result system. out. println (value. a); system. out. println (value. b);} // The parameter is Private Static void getvalue (myvalue v) {v. A = 24; V. B = 57;} // The parameter is of the standard type Private Static void getvalue (integer a, integer B) {A = 17; B = 37 ;}} // custom class myvalue {// variable initialization public int A = 0; Public int B = 0 ;}

After the getvalue (myvalue) parameter is assigned a value in the function, the result is still taken out through the class, and the variables in the class have changed.

Although the parameters of getvalue (integer, integer) have changed in the function, the input parameters are not changed after the function is returned. Secondly, the string type cannot bring back the results through parameters,Only objects created through new.

 

String, you can use string [] as a parameter, or return the running results in the function.

 

 
Public static void main (string [] ARGs) {try {string [] STR = new string [1]; STR [0] = ""; Test2 (STR ); // The Test2 value is assigned to the result system. out. print (STR [0]);} catch (exception e) {e. printstacktrace () ;}} Private Static void Test2 (string [] Str) throws exception {STR [0] = "Hello world. ";}

 

 

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.