Introduction to Java (5) Classification of methods and parameter passing methods

In a class, methods can also be divided into two categories: instance methods and class methods (class methods are modified with static, called static methods).
In class methods, instance methods and instance variables of a class cannot be directly accessed. Because class methods can be accessed without instantiation, and instance variables need to be instantiated to allocate heap memory, so instance methods and instance variables without instantiation cannot be accessed.

one,

insert image description here
Similarly, when accessing class methods, you can use the two methods of "class name. method name (parameter list)" or "instantiation. method name (parameter list)", that is, classes can access class methods without instantiation.
When accessing the instantiation method, an object must be instantiated first, and then the instance method is called in the way of "instantiation. instance method (parameter list)".
insert image description here
In object-oriented programming, objects communicate with each other by passing messages to simulate the connection between different things in the real world. In java, passing a message to an object is achieved by passing parameters to the corresponding method.
Parameter passing is divided into two types: basic type parameter passing and reference type parameter passing.
The basic type of parameter passing is when an actual parameter is passed to a method, if the parameter value is changed during the execution of the method, only the formal parameter is changed, and the original actual parameter value remains unchanged.
Reference type parameter passing is when a parameter is passed to a method, the method receives the value of the reference variable, which is the memory address of an object.
Of course, both basic type parameter passing and reference type parameter passing belong to value passing.

two,

一个基本类型参数传递,我们看到在执行完swap()方法后a、b的值没改变
insert image description here
insert image description here
代码解释:
第3行: main函数,程序的入口
第4行:声明int类型变量a,b并赋值1,2
第5行:控制台输出调用swap(方法前a,b的值
第6行:调用swap(方法,并将变量a, b的值当做参数传递给swap(
第9行:此时程序跳转到第9行执行函数swap,并获取形参值a=1,b=2
第10行:证明变量temp并将a值赋值给temp
第11行:将b值赋值给a
第12行:将temp储存的a值赋值给b
第13行:输出a, b值,观察结果
第7行:程序执行完swap(方法后跳转会第6行,紧接着执行第7行,输出a, b的值。

three,

insert image description hereinsert image description here
The first thing to explain is that in a java program file (java), there may be multiple classes, but at most one can only be of
public type, that is, a common class, and the name of the common class must be consistent with the name of the program file.
We all know that when an object is instantiated, an address in memory is returned, that is, b1, b2
does not store data 1 and 2, but the address where the data is stored.
When executing the swap(b1, b2) method, it is equivalent to pointing the formal parameters a and b of the swap() method to the memory addresses where b1 and b2 store data
. Therefore, after the numbers stored in it are exchanged in the swap() method, b1 and b2 also change accordingly.
For the detailed meaning of the code in the figure, you can refer to the content of my previous columns, so I won’t go into details here.

Guess you like

Origin blog.csdn.net/sand_wich/article/details/105148617