简体   繁体   English

Java方法中byte []和byte ...之间的区别

[英]Difference between byte[] and byte … in Java Methods

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. 我无法自信地回答他们,也不记得......被称为是什么。

The ... in your first example are called varargs . 你的第一个例子中的...被称为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. 当你想要为方法硬编码可变数量的参数但又不想手动创建一个数组来保存它们时,Varargs是一种方便。 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. 第一个例子不那么混乱,更具可读性。

The parameters with ellipses are generally referred to as "varargs" if you want to google that. 如果你想谷歌那么,带椭圆的参数通常被称为“varargs”。

Using varargs allows you to call a method with variable number of arguments without having to specify an array eg 使用varargs允许您调用具有可变数量参数的方法,而无需指定数组,例如

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. 因此varargs允许一定程度的便利性,但有缺点 - varargs参数必须是方法签名中的最后一个参数,因此您不能为方法提供多个varargs参数。 If you want to pass multiple arrays to a method you have to use arrays, not varargs. 如果要将多个数组传递给方法,则必须使用数组,而不是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. 您可能在某些可能期望varargs的地方看到数组的另一个原因是varargs仅在Java 5中引入 - 较旧的代码和需要向后兼容的代码仍将使用数组,即使在概念上使用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的优点是灵活性 - 在某些情况下,调用者无论如何都会准备好数组,有些情况下他们只有几个参数。 Varargs will accept either the array or each variable as a separate argument, saving the caller the trouble of instantiating and populating an array. Varargs将接受数组或每个变量作为单独的参数,从而为调用者省去了实例化和填充数组的麻烦。

The ellipsis (three dots) indicates that you are using "varargs". 省略号(三个点)表示您正在使用“varargs”。

See http://download.oracle.com/javase/1,5.0/docs/guide/language/varargs.html for more details. 有关详细信息,请参阅http://download.oracle.com/javase/1,5.0/docs/guide/language/varargs.html

Inside the method, you access the elements of "send" as an array. 在方法内部,您可以作为数组访问“send”元素。 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. 在第二个putMessage中,调用者被迫创建一个字节数组以传递给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. 在第一个putMessage中,调用者可以简单地说“putMessage(byte1,byte2)”或“putMessage(byte1,byte2,byte3)”或“putMessage(byte1)” - 可变数量的参数或varargs。

The first one is with Varargs . 第一个是Varargs

In short 简而言之

A. First can be used to call with single byte type arg, 2 byte args.. or many args or an array. A.首先可以用于调用单字节类型arg,2字节args ..或许多args或数组。

B. second will be used with array only. B.秒将仅与数组一起使用。

The ellipses (...) allow you to inline N parameters of a type to a function call without having to define an array first. 省略号(...)允许您将类型的N个参数内联到函数调用,而无需先定义数组。 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. 这是省略号语法(可变长度参数列表)的一个很好的例子 。在查看示例时,如果使用数组而不是可变长度参数列表,请考虑客户端代码(在主函数中)的样子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 byte []和List之间有什么区别 <Byte> 在Java? - What is the difference between byte[] and List<Byte> in Java? Java中文件结尾与字节值-1之间的差异? - Difference between the end of file and a byte value of -1 in Java? 从 TCP 服务器读取字节的方法之间的区别? - Difference between methods to read a byte from TCP server? Java中InputStream.read(byte b)和BufferedInputStream的区别 - Difference between InputStream.read(byte b) and BufferedInputStream in Java JAVA:字节码和二进制文件有什么区别? - JAVA: What's the difference between byte code and binary? 在Java中使用ASCII和十六进制格式初始化字节之间有什么区别吗? - Is there any difference between initializing a byte with ASCII and Hex form in Java? Java读取文件,字节流和字符流之间的性能差异 - Java-reading a file, performance difference between Byte and Character Streams 将DateTime转换为字节数组时C#和Java之间的区别 - Difference between C# and Java when converting DateTime to byte array 从 C# 和 Java 中的字符串获取字节数组之间的区别 - Difference between get byte array from string in C# and Java Java中的字符流和字节流有什么区别?C中的字符串和字节与字节有什么区别? - What's the difference Between Character Stream and Byte Stream in Java and Char vs Byte in C?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM