The previous section describes the differences between the assignment methods of class type variables and basic variables. This section describes the differences between class type parameters and basic type parameters.
First, you must understand what is a class type parameter. Class type parameter. The method definition start position. The formal parameter is given in parentheses after the method name. Similar to the class type variable, the class type parameter is used as a local variable and stores the memory address of the class type object. The value of the instance variable of the class type parameter can be changed in the method, but the value of the basic type parameter cannot. The following section describes the Code:
/*** Comments: the impact of the method on the value of the class type parameter and the value of the basic type parameter * @ author plug-in name * Create Time: 2013-09-15 **/public class ClassTest {private int num; public int getNum () {return num;} public void setNum (int num) {this. num = num;} public void change (int n) {n = 2;} public void change (ClassTest n) {n. setNum (2);} public void change (ClassTest c1, ClassTest c2) {c2 = c1;} public static void main (String [] args) {ClassTest class1 = new ClassTest (); class1.setNum (1); class1.change (class1); // input class type class1, where class1.getNum = 1 System. out. println (class1.getNum (); // output class type class1, where class1.getNum = 2 class1.setNum (1); class1.change (class1.getNum (); // input basic class class1.getNum = 1 System. out. println (class1.getNum (); // output basic type class1.getNum = 1 class1.setNum (2); ClassTest class2 = new ClassTest (); class2.setNum (1); class2.change (class1, class2 ); // input class type class1, where class1.getNum = 2 // input class type class2, where class2.getNum = 1 System. out. println (class2.getNum (); // output class type class2, where class2.getNum = 1 }}
It can be seen from the code that the value of the class type parameter changes in the method and directly affects the value of the parameter outside the method, but the value of the basic type parameter is not modified. In addition, when the method replaces an object directly with another object, it also fails.
Note: The method is called a parameter, and the method is called a parameter ).
This article is from the blog "plug on the name of pigs", please be sure to keep this source http://zuohao1990.blog.51cto.com/6057850/1297313