您的位置 首页 java

《Java面向对象程序设计》——参数传值

《Java面向对象程序设计》——参数传值

今天的主要内容是Java中的参数传值。

当方法被调用时,如果方法有参数,参数必须要实例化,即参数变量必须有具体的值。在Java中,方法的所有参数都是“传值”的,也就是说,方法中参数变量的值是调用者指定值的一个副本。

Today’s main topic is passing values for parameters in Java

When a method is called, if the method has parameters, the parameters must be instantiated, i.e., the parameter variables must have specific values. In Java, all parameters of a method are “passed”, that is, the value of the parameter variable in the method is a copy of the value specified by the caller.

01

基本数据类型参数的传值

对于基本数据类型的参数,向该参数传递的值的级别不可以高于该参数的级别。例如,不可以向int型参数传递一个float值,但可以向double型参数传递一个float值。

For parameters of basic data types, the level of the value passed to the parameter may not be higher than the level of the parameter. For example, it is not possible to pass a float value to an int parameter, but it is possible to pass a float value to a double parameter.

02

引用类型参数的传值

《Java面向对象程序设计》——参数传值

Java的引用型数据包括对象,数组以及接口。当参数是引用类型时,“传值”传递的是变量中存放的“引用”,而不是变量所引用的实体。

对于两个同类型的引用型变量,如果具有同样的引用,就会用同样的实体,因此,如果改变参数变量所引用的实体,就会导致原变量的实体发生同样的变化。

Java’s reference data includes objects, arrays, and interfaces. When the parameter is a reference type, the “value passed” is the “reference” stored in the variable, not the entity referenced by the variable.

For two reference variables of the same type, if they have the same reference, they will use the same entity, so if you change the entity referenced by the parameter variable, you will cause the entity of the original variable to change in the same way.

03

可变参数

可变参数是指在声明方法时不给出参数列表中从某项直至最后一项参数的名字和个数,但这些参数的类型必须相同。可变参数使用“…”表示若干个参数,这些参数的类型必须相同,最后一个参数必须是参数列表中的最后一个参数。例如:public void f(int …x)那么,在方法f的参数列表中,从第一个至最后一个参数都是int型,但连续出现的int型参数的个数不确定,称x是方法f的参数列表中可变参数的“参数代表”。参数代表可以通过下标运算来表示参数列表中的具体参数,即x【0】,x【1】,x【2】……x【m】分别表示x代表的第1~m个参数。

Variable parameters are methods that are declared without giving the name and number of parameters in the parameter list from a particular item to the last one, but the type of these parameters must be the same. Variable parameters use “…” to indicate a number of parameters, which must be of the same type, and the last parameter must be the last parameter in the parameter list. For example: public void f (int …x) Then, in the parameter list of method f, from the first to the last parameter is int type, but the number of consecutive parameters of int type is not determined, and x is said to be the “parameter representative” of variable parameters in the parameter list of method f. “. The parameter representative can be subscripted to represent the specific parameters in the parameter list, i.e. x [0], x [1], x [2] ……x [m] represent the 1st to mth parameter of x representative respectively.

参考资料:文字:百度;图片:微博;翻译:百度翻译

本文由LearningYard新学苑原创,部分图片文字来自网络,如有侵权请联系。

文章来源:智云一二三科技

文章标题:《Java面向对象程序设计》——参数传值

文章地址:https://www.zhihuclub.com/177651.shtml

关于作者: 智云科技

热门文章

网站地图