Difference between byte[] and byte … in Java Methods

和自甴很熟 提交于 2021-02-08 19:45:12

问题


Someone asked me what the difference between the two method parameters and why you would use the ... over specifically assigned array.

putMessage(byte ...send)

putMessage(byte[] send)

I couldn't answer them with confidence and couldn't remember what the ... is called.


回答1:


The first one is with Varargs.

In short

A. First can be used to call with single byte type arg, 2 byte args.. or many args or an array.

B. second will be used with array only.




回答2:


The ... in your first example are called varargs. Your second example has an array argument. Varargs are a convenience for times when you want to hard code a variable number of arguments to a method but don't want to manually create an array to hold them. It's a shorthand notation. Consider this:

putMessage(0b00100101, 0b00100101, 0b00100101); // varargs

vs. this:

putMessage(new byte[] { 0b00100101, 0b00100101, 0b00100101 }); // array

The first example is less cluttered and more readable.




回答3:


The parameters with ellipses are generally referred to as "varargs" if you want to google that.

Using varargs allows you to call a method with variable number of arguments without having to specify an array e.g.

public void printStr(String ...strings) {
    for (String s : strings) {
        System.out.println(s);
    }
}

> printStr("Hello", "World")
Hello
World

So varargs allow a certain degree of convenience, but there are downsides - the varargs parameter must be the last parameter in the method signature, and thus you cannot have more than one varargs parameter to a method. If you want to pass multiple arrays to a method you have to use arrays, not varargs.

Another reason you might see arrays in some places where you might expect varargs is that varargs were only introduced in Java 5 - older code and code that needs to be backwards compatible will still be using arrays even where it might make more sense conceptually to use varargs.

The advantage of using varargs in the method signature is flexibility - there are some situations where the caller will have an array ready anyway and some where they will just have several arguments. Varargs will accept either the array or each variable as a separate argument, saving the caller the trouble of instantiating and populating an array.




回答4:


The ellipsis (three dots) indicates that you are using "varargs".

See http://download.oracle.com/javase/1,5.0/docs/guide/language/varargs.html for more details.

Inside the method, you access the elements of "send" as an array. The two methods are the same in that regard. The convenience is for the caller. In the second putMessage, the caller is compelled to create an array of bytes to pass to putMessage. In the first putMessage, the caller can simply say "putMessage(byte1, byte2)" or "putMessage(byte1, byte2, byte3)" or "putMessage(byte1)" -- variable number of arguments, or varargs.




回答5:


The ellipses (...) allow you to inline N parameters of a type to a function call without having to define an array first. In the end you do simply get an array of parameters but it's basically shorthand or syntactic sugar. Also your client code might be a little cleaner and more declarative with the ellipses syntax... though it could easily go the other way and become mucky and unreadable.

Here's a great example of the ellipses syntax (variable length argument lists.) While looking at the sample consider what the client code (in the main function) would look like if an array was used instead of a variable length argument list.



来源:https://stackoverflow.com/questions/7637893/difference-between-byte-and-byte-in-java-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!