Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the variable parameter method of Java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge points about how to use the variable parameter method of Java. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

I. definition of variable parameter method

First, take a look at how the variable parameter method is defined in the code, as follows:

Public static void method1 (Integer id, String... Names) {System.out.println ("id:" + id + "names:" + names.length);}

From the above example, we can see that when defining a method, add three points after the last parameter type. Means that the parameter can accept multiple parameter values of the same type, and multiple parameter values are passed as an array

Here are a few points we need to pay attention to:

A variable parameter can only be used as the last argument of a function, with or without any other parameters before it.

Since a variable parameter must be the last parameter, a function can only have at most one variable parameter

The variable parameters of Java will be transformed into an array by the compiler

It is mentioned above that the form of variable parameters will be compiled into an array, so the question is, can I write two of the following methods?

Public static void method1 (Integer id, String... Names) {System.out.println ("id:" + id + "names:" + names.length);} public static void method1 (Integer id, String [] names) {System.out.println ("id:" + id + "names:" + names.length);}

A method that defines a variable parameter with the same name and a method that contains an array in a class. After writing, we find that IDEA has prompted us that this way of writing can not be compiled.

From this we can know that the variable parameters will appear as an array in the method signature after being compiled into bytecode, so that the signatures of the two methods are the same, if they appear at the same time, they cannot be compiled and passed.

Second, the invocation of variable parameter method

The call of a variable parameter method is no different from that of other methods. Here, in addition to calling through variable parameters, we can also call it in the form of an array, as shown below:

Public static void main (String [] args) {/ / directly pass parameters method1 (1, "ziyou", "java geek technology"); / / pass parameters String [] array = new String [] {"ziyou", "Java geek technology", "fdf"}; method1 (2, array); / / do not pass variable parameters method1 (3);}

The two invocation forms are essentially the same in the form of variable arguments and arrays; in addition, the number of variable parameters can also be 0.

III. Overloading of variable parameter methods

Imagine what would happen if we defined the following two methods, defined and used.

Public static void method2 (String... Names) {System.out.println ("111111");} public static void method2 (String value1, String value2) {System.out.println ("22222");}

The first is a method with only one variable parameter; the second is a method with a fixed parameter of type String and the second parameter is a variable parameter. First of all, there is no problem with the definition, there is no error prompt for IDEA, and there is no problem with compilation.

What about when using it? For example, what will be the result of writing like this?

Public static void main (String [] args) {method2 ("java geek technology", "ziyou");

Before looking at the output, we can see that the calls in the main function, in fact, these two overloaded functions can be satisfied, and there is nothing wrong with the compilation, so what will the program output?

From the actual running result, we can see that the output result is 22222, which means that the method2 (String value1, String value2) method is running. What does that mean?

It shows that when there is an overloaded method formed with a variable parameter method, the method with fixed parameters will be given priority to be executed, which I believe we have never paid attention to.

At this point, there may be Xiaoming to ask, what if the value2 in our second method is also a variable parameter? What will happen to this situation? For this reason, let's take another look at what happens in the following form.

Public static void method2 (String... Names) {System.out.println ("111111");} public static void method2 (String value1, String value2) {System.out.println ("22222");} public static void method2 (String value1, String...) Value2) {System.out.println ("33333");}

First of all, there is no error prompt for IDEA when it is defined, indicating that there is no problem with compilation, but what about when it is called?

You can see that at this time, IDEA has prompted us to match multiple suitable methods, which cannot be compiled and passed, mainly due to the first and third ways of writing, matching to multiple methods with variable parameters. We should pay attention to this problem in our daily development.

4. Object variable parameters

Seeing that Xiao Ming is about to ask, can I create a variable parameter method based on Object, so that this method can accept all types of parameters? It's like this:

Public static void method3 (Object... Objects) {System.out.println ("objects size" + objects.length);} above is all the content of the article "how to use the variable parameter method of Java". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report