java变量_Java变量

java变量

Java varargs was introduced in Java 1.5. Java varargs is also known as java variable arguments.

Java varargs是Java 1.5中引入的。 Java varargs也称为Java变量参数

Java变量 (Java varargs)

varargs in java enables a method to accept variable number of arguments. We use three dots (…) also known as ellipsis in the method signature to make it accept variable arguments. For example;

Java中的varargs使方法可以接受可变数量的参数。 我们在方法签名中使用三个点(…)(也称为省略号)以使其接受可变参数。 例如;

public static int sum(int i, int...js ){
//do something
}

关于Java中的varargs的要点 (Important points about varargs in java)

Few points to know about varargs in java are;

关于Java中的varargs的几点知识是:

  1. We can have only one varargs in the method.

    该方法中只能有一个可变参数。
  2. Only the last argument of a method can be varargs.

    方法的最后一个参数只能是varargs。
  3. According to java documentation, we should not overload a varargs method. We will see why it’s not a good idea.

    根据Java文档,我们不应重载varargs方法。 我们将看到为什么它不是一个好主意。

java varargs如何工作? (How java varargs work?)

When we invoke a method with variable arguments, java compiler matches the arguments from left to right. Once it reaches to the last varargs parameter, it creates an array of the remaining arguments and pass it to the method. In fact varargs parameter behaves like an array of the specified type.

当我们调用带有可变参数的方法时,java编译器从左到右匹配参数。 一旦到达最后一个varargs参数,它将创建剩余参数的数组并将其传递给方法。 实际上,varargs参数的行为类似于指定类型的数组。

//method with variable arguments
public static int sum(int i, int...js ){
    int sum = i;
    for(int x : js){
        sum+=x;
    }
    return sum;
}

//method with same implementation as sum with array as argument
public static int sumArray(int i, int[] js ){
    int sum = i;
    for(int x : js){
        sum+=x;
    }
    return sum;
}

If you will look at both sum and sumArray methods, you will see that the implementation body is exactly same. So we should use varargs when API offers them, for example java.io.PrintStream.printf() method but we should not take it as a replacement for array.

如果同时查看sumsumArray方法,您将看到实现主体是完全相同的。 因此,当API提供它们时,我们应该使用varargs,例如java.io.PrintStream.printf()方法,但我们不应该将其用作数组的替代品。

为什么我们不应该重载varargs方法 (Why we should not overload varargs method)

Let’s look at an example why overloading java varargs method is not a good idea.

让我们看一个示例,为什么重载java varargs方法不是一个好主意。

package com.journaldev.misc;

public class VarargsExample {

    public static void main(String[] args) {
        System.out.println(sum(1));
        System.out.println(sum(1,2)); //compiler error, ambiguous method

    }

    public static int sum(int i, int...js ){
        System.out.println("sum1 called");
        int sum = i;
        for(int x : js){
            sum+=x;
        }
        return sum;
    }
    public static int sum(int i, int k, Object...js ){
        System.out.println("sum2 called");
        int sum = i+k;
        for(Object x : js){
            sum+=1;
        }
        return sum;
    }
    
}

In above example, you will notice that compiler will not complain when we overload methods with varargs. But when we try to use it, compiler get’s confused which method to use when mapping the second argument.

在上面的示例中,您会注意到当我们使用varargs重载方法时,编译器不会抱怨。 但是,当我们尝试使用它时,编译器get会混淆在映射第二个参数时使用哪种方法。

If there is only one argument, compiler is smart to use first method because it can work with minimum one argument but second method needs at least two arguments. Below image from Eclipse shows the error message as The method sum(int, int[]) is ambiguous for the type VarargsExample.

如果只有一个参数,则编译器很聪明地使用第一个方法,因为它可以使用最少一个参数,但是第二个方法至少需要两个参数。 下图来自Eclipse,显示错误消息为The method sum(int, int[]) is ambiguous for the type VarargsExample

That’s all about java varargs. It’s good to know feature but you don’t need to use it when writing code yourself.

这就是关于Java varargs的全部内容。 知道功能很好,但是您自己编写代码时不需要使用它。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/1257/java-varargs

java变量

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值