Passing Java learning values

Source: Internet
Author: User
Value Transfer of Java learning-general Linux technology-Linux programming and kernel information. The following is a detailed description. Value passing is a function that calls another function, including passing parameters. Often, value passing is to pass data to another function for further processing, so as to clearly display the expression meaning of the function. When calling a function, there will be data transmission. We also need to consider the problem of form parameters and real parameters, as shown in the following program:

Public class PassTest

{

Float ptValue;

Public void changeInt (int value)

{

Value = 55;

}

Public void changeStr (String value)

{

Value = new String ("different ");

}

Public void changeObjValue (PassTest ref)

{

Ref. ptValue = 99f;

}

Public static void main (String args [])

{

String str;

Intval;

PassTest pt = new PassTest ();

Val = 11;

Pt. changeInt (val );

System. out. println ("Int value is:" + val );

Str = new String ("Hello ");

Pt. changeStr (str );

System. out. println ("Str value is:" + str );

Pt. ptValue = 101f;

Pt. changeObjValue (pt );

System. out. println ("Current ptValue is:" + pt. ptValue );

}

}

The output result of this program is:

11

Hello

99f

The first 11 and Hello follow the parameter transfer principle. Although the parameter value is changed, the real parameter is not changed, which is the same as that in C. So what should I do if I want to change the value of the real parameter in the function? We know that in C, we can use pointer variables and pointer variables as function parameters. In this way, the address of the parameter is passed, when the parameter value of the same address is changed in the function, no error will occur. However, if there is no pointer in java, the above C language approach should be implemented, what should I do? We know that objects are taken into account everywhere in java, and handles are used to operate the activity of objects. If we pass the object handle to the function, you can point the two to the same handle (that is, the address), so that the data can be modified. As you can see from the above program, pt is the PassTest object created in the main function. By passing it, the address can be passed. In the subfunction, the ref and pt of the main function actually point to a memory unit. Then, modify the ptValue value in ref, that is, the ptValue value in pt, even if no final data is returned, this data will also change.

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.