English 中文(简体)
1. Java方法中的 by与 by之间的区别
原标题:Difference between byte[] and byte ... in Java Methods

有些人问我,这两种方法参数之间有何区别,为什么你将使用......而不是专用阵列。

putMessage(byte ...send)

putMessage(byte[] send)

我 could信心地回答这些问题,不要忘记......所说的话。

最佳回答
问题回答

rel=“nofollow”>。 你的第二个例子有各种论点。 有时,如果你想要硬性地制定一种方法的各种不同论点,那么瓦拉格斯就是一个方便之举,但不想人工制造一个阵列来加以控制。 它带有短距离。 考虑:

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.

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允许一定程度的方便,但也有下方,旋转翼参数必须是方法签名的最后参数,因此,你不能用一个以上的旋转参数。 如果你想通过多种阵列,而采用的方法是,你必须使用阵列而不是加固。

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.

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.

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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签