Java Reference Data types

Source: Internet
Author: User
Tags instance method java reference

Value passing: When a method is called, the actual parameter passes its value to the corresponding formal parameter, and the change of the formal parameter value in the method execution does not affect the value of the actual parameter.
Reference delivery: Also known as a pass-through address. When a method is called, the reference to the actual parameter (the address, not the value of the parameter) is passed to the corresponding formal parameter in the method, and in the execution of the method, the operation of the formal parameter is actually the operation of the actual parameter, and the change of the parameter value in the execution of the method will affect the value of the actual parameter.
The following examples illustrate:
Pass-through ---Basic data type parameters
public class passvalue{
Static
void Exchange (int a, int b) {//static method, exchanging a value of a, a, a
int temp;
Temp
= A;
A = b;
b = temp;
}
public static void
Main (string[] args) {
int i = 10;
Int J =
100;
System.out.println ("Before Call:" + "i=" + i + "\ T" + "J =" +
j);//Pre-call
Exchange (I,
j); Value is passed, the main method can only call static methods
System.out.println ("After
Call: "+" i= "+ i +" \ T "+" J = "+
j);//After Call
}
}
Operation Result:
Before call:i = Ten J =
100
After call:i = ten j = 100
Description: Call Exchange (I,
j), the actual parameter i,j the value to the corresponding form parameter, a, B, when executing the method Exchange (), the change of the value of the formal parameter, a, B, does not affect the values of the actual parameters I and J, and the values of I and J have not changed before and after the call.
reference to pass---object as a parameter
When a method invokes an object (or array) as an argument in a method, the argument passes the reference (address) of the object, that is, when the method is called, the actual parameter passes the reference (address) of the object to the formal parameter. This is the actual parameter and the form parameter point to the same address, that is, the same object (array), when the method executes, the change of the formal parameter is actually a change to the actual parameters, the result is preserved after the call is over.
Class
book{
String name;
Private Folat Price;
Book (String
N, float) {//constructor method
name = N;
Price =
P
}
static void Change (book A_book, String N, float
p) {//static method, object as parameter
A_book.name = n;
A_book.price
= P;
}
public void
Output () {//instance method, outputs object information
System.out.println ("Name:" + name +
"\ T" + "Price:" + price);
}
}
public class
passaddr{
public static void Main (String [] args) {
Book B =
New book ("Java2", 32.5f);
System.out.print ("Before
Call:\t "); Before calling
B.output ();
B.change (b, "C + +", 45.5f); Reference passing, passing the reference to object B, modifying the value of object B
System.out.print ("After
Call:\t "); After the call
B.output ();
}
}
Operation Result:
Before Call:name:java2 price:32.5
After call:name:c++ price:45.5
Note: When you call change (b, "C + +", 45.5f), object B is used as the actual parameter, the reference is passed to the corresponding formal parameter a_book, in fact A_book also points to the same object, that is, the object has two reference names: B and A_book. When you execute a method change (), the A_book operation on the formal parameter is the operation of the actual parameter B.

Java Reference Data types

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.