The value passing mechanism of Java

Source: Internet
Author: User

Key points and difficulties


* Method parameter transfer (emphasis, difficulty)
* 1. Formal parameters: method declaration, parameter in method parentheses
* Argument: The value of the actual parameter passed in when the method is called
*
* 2. Rule: Parameter passing mechanism in Java: value passing mechanism
* 1) A parameter is a basic data type: A variable that passes the value of an argument to the base data type of the parameter
* 2) Formal parameters are reference data types: The value of the argument's reference type variable (the first address value of the object entity corresponding to the heap space) is passed to the reference type variable of the formal parameter.

"Typical Example 1"

Public static void Main (string[] args) {
testargstransfer tt = new Testargstransfer ();

int i = ten;
int j = 5;
System.out.println ("I:" + i + "J:" + j);//i:10 J:5

////Exchange Variables I and J values
//int temp = i;
//i = j;
//j = temp;
Tt.swap (i, j);//Pass the value of I to the value of M,j to n


System.out.println ("I:" + i + "J:" + j);//i:10 J:5

}
//Define a method that swaps the values of two variables
Public void Swap (int m,int n) {
int temp = m;
m = n;
n = temp;
System.out.println ("m:" + M + "N:" + N);

}

After using the Swap method:

"Typical Example 2"
Public class TestArgsTransfer1 {
Public static void Main (string[] args) {
TestArgsTransfer1 tt = new TestArgsTransfer1 ();
dataswap ds = new Dataswap ();

System.out.println ("ds.i:" + ds.i + "DS.J:" + ds.j);

Tt.swap (DS);
System.out.println (DS);

System.out.println ("ds.i:" + ds.i + "DS.J:" + ds.j);

}
//Exchange The value of the element
Public void Swap (Dataswap d) {
int temp = D.I;
d.i = D.J;
D.J = temp;
System.out.println (d);//Print the value of the reference variable D
}
}

Class dataswap{
int i = 10;
int j = 5;
}

The value passing mechanism of Java

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.