Java class type parameters

Source: Internet
Author: User

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

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.