java:How to use variable parameter? (easy to understand)

简介: It's the first time to write a blog in English for me. (About variable parameter)

Catalogue:

I.Preface :

II.Syntax :

III.NOTICE(significant) :

IV.Code Demonstrate :

       1.Show notice one, notice two and notice three :

       2.Show notice four :

       3.Show notice five :

       4.Exercise:


I.Preface :

       java allows us to encapsulate many methodswhich have the same name and function but have different number of parametersinto one method  at one class.

       We achieve it by using variable parameter in the list of formal parameters.

       By the way, we will use variable parameter in the module java reflect, etc.

II.Syntax :

       access_modifierreturn_typemethod_name (data_structures... parameters) {

   

       }

       ΔIn the list of formal parameters, the format of three points "..." is fix.

III.NOTICE(significant) :

       1. The number of actual parameter can range from 0 to n.(n = 1, 2, 3, 4, 5......)

       2. The actual parameter can be an array directly.

       3. The variable parameter is actually an array, so you can use it as an array.

       4. When variable parameter is used with ordinary parameter, make sure that the             variable parameter is at last of the list of formal parameter.

       5. There could be only one variable parameter at one parameter list.

IV.Code Demonstrate :

       Hey, guys, I am about to demonstrate all the notices mentioned above in code.

       1.Show notice one, notice two and notice three :

packagecsdn.varparameter;
publicclassVariableParameter {
publicstaticvoidmain(String[] args) {
Test1t1=newTest1();
int[] array1= {2, 11, 211, 985, 141};
//1. The number of actual parameter can range from 0 to n.(n = 1, 2, 3, 4, 5......)System.out.println(t1.getSum(0, 1, 2, 3, 4, 5));
System.out.println(t1.getSum(array1));
//2. The actual parameter can be an array directly.System.out.println("------------------------");
    }
}
classTest1 {
publicintgetSum(int... parameters) {
System.out.println(parameters.length+" parameters were received.");
//3. The variable parameter is actually an array, so you can use it as an array.intnum=0;
for (inti=0; i<parameters.length; ++i) {
num+=parameters[i];
        }
System.out.print("The Sum of parameters passed is : ");
returnnum;
    }
}

image.gif

       Output result :

image.png

       2.Show notice four :

packagecsdn.varparameter;
publicclassVariableParameter {
publicstaticvoidmain(String[] args) {
Test2t2=newTest2();
int[] array1= {2, 11, 211, 985, 141};
int[] array2= {1, 2, 7};
System.out.println(t2.getSum2(array1, 1, 3, 1));
System.out.println(t2.getSum2(array1, array2));
System.out.println("------------------------");
    }
}
/*4. When variable parameter is used with ordinary parameter, make sure that the variable parameter is at last of the list of formal parameter.*/classTest2 {
publicintgetSum2(int[] arr, int... parameters) {
System.out.println(parameters.length+" parameters were received.");
intsum1=0;
intsum2=0;
for (inti=0; i<arr.length; ++i) {
sum1+=arr[i];
        }   //Use sum1 variable to store the sum of "arr" array.for (inti=0; i<parameters.length; ++i) {
sum2+=parameters[i];
        }   //Use sum2 variable to store the sum of "variable parameter".System.out.print("The total sum of parameters passed is : ");
returnsum1+sum2;
    }
}

image.gif

       Output result :

image.png

       3.Show notice five :

        Actually it's easy to understand the notice five, because we have got the notice four :  When variable parameter is used with ordinary parameter, make sure that the variable parameter is at last of the list of formal parameter. So, if you have variable parameters, more than one, you can't promise that every variable parameters is at the end of the the list of formal parameters. Absolutely you can't achieve it!

       Let's have a look at the demonstration,a GIF picture, as follow:

image.png

       Apparently, we can see the IDEA report a error, and the reason is actually what we're talking about above.        

       4.Exercise:

      requirement : Encapsulate one static method in a arbitrary class, whatever, and the formal parameters you need to pass in are Student's name, Student's grade and the scores of his or her every subjects.We supposed that the Student totally examines five subject. When this method is invoked, the student's name, grade and five subjects' sum scores will be printed on the console.

       Code Demonstration :

packagecsdn.varparameter;
publicclassVariableParameter {
publicstaticvoidmain(String[] args) {
System.out.println(Test3.studentScore("Cyan", 14, 141,135,80,75));
    }
}
classTest3 {
publicstaticStringstudentScore(Stringname, intgrade, int... scores) {
intscoreNum=0;
for (inti=0; i<scores.length; ++i) {
scoreNum+=scores[i];
        }
returnname+", who is in "+grade+" class, got totally "+scoreNum+" points";
    }
}

image.gif

       Output result :

image.png    

System.out.println("END---------------------------------------------------------------------");

目录
相关文章
|
Java Windows
windows下 安装 Elasticsearch报错warning: usage of JAVA_HOME is deprecated, use ES_JAVA_HOME
windows下 安装 Elasticsearch报错warning: usage of JAVA_HOME is deprecated, use ES_JAVA_HOME
966 0
|
8月前
|
网络协议 Java Shell
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
470 7
|
Java
JAVA 端口被占用 报错解决方案:java.net.BindException: Address already in use: bind
JAVA 端口被占用 报错解决方案:java.net.BindException: Address already in use: bind
829 0
|
Java
springboot 异常java.net.BindException: Address already in use: bind
springboot 异常java.net.BindException: Address already in use: bind
340 0
|
Java
ElasticSearch启动报错 java version is an early-access build ,only use release builds【已解决】
ElasticSearch启动报错 java version is an early-access build ,only use release builds【已解决】
142 0
|
Java
【Java异常】com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: can not use this method fo
【Java异常】com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: can not use this method fo
960 0
java.net.BindException: Address already in use: 解决方法
java.net.BindException: Address already in use: 解决方法
1307 0
|
Java
java:How to use code block?(Detailed)
This article will share something about java's code block. Very detailed, I promise.
195 0
java:How to use code block?(Detailed)
|
Oracle 关系型数据库 应用服务中间件
java.net.BindException: Address already in use: JVM_Bind
java.net.BindException: Address already in use: JVM_Bind
405 0
|
Java Nacos
java:nacos集群部署报错Address already in use
java:nacos集群部署报错Address already in use
409 0