Two-part exchange code easy to understand Java parameter passing mechanism

Source: Internet
Author: User
Tags java web

One, Java is divided into two types of memory:

1, stack memory, used to store the basic types of variables and object reference variables;

2, heap memory, used to store the new keyword to create objects and arrays;

Two, Java method of parameter transfer mode only one-value transfer, that is, the actual parameters are copied into the method, the actual parameters themselves will not be any impact, reference to the C language function parameters passed, the form of participation in the actual parameters (at the time when the study is really good halo);

Third, the first paragraph of the Exchange code (basic type parameters)

public class Javatest {public
	static void (int a, int b)
	{
		//Exchange a,b value
		int temp = A;
		A = b;
		b = temp;
		System.out.println ("After swapping, the values of A and B in the swap method are:" + A + "  " + b);
	}
	public static void Main (string[] args)
	{
		int a = 3;
		int b = 9;
		Swap (A, b);
		System.out.println ("After the exchange is performed, the values of A and B in the main method are:" + A + "  " + b);
	}

Execution results:


Schematic diagram:


The second paragraph Exchange code (object is parameter)

Class Base
{public
	int A;
	public int b;
}

public class Javatest {
	the value of the A,b property in the public static void swap (base base) {
		//Exchange base Object
		int temp = BASE.A;
		Base.a = base.b;
		base.b = temp;
		System.out.println ("After the swap, the value of the A and B properties of the base object in the Swap method is" + Base.a + "" + base.b);
	}
	public static void Main (string[] args)
	{
		base base = new Base ();
		Base.a = 3;
		base.b = 9;
		Swap (base);
		System.out.println ("After the exchange is performed, the value of the A and B properties of the base object in the Main method are:" + Base.a + "" + base.b);
	}
Execution results:


Schematic diagram:




(Recommend a big God's blog, aloof Wolf's blog, http://www.cnblogs.com/xdp-gacl/

His blog covers the Java and Java Web Nearly all important will be used to explain the basics, I think learning to be more sharing, I am learning the Java Web, I hope to communicate with you, thank you


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.