迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

Java 默认参数

作者:迹忆客 最近更新:2023/10/19 浏览次数:

本教程介绍了如何在 Java 中实现默认参数。

如果不传递任何值,则使用默认参数。当我们要在方法接受多个参数的同时传递有限的参数时,这将很有帮助。例如,一个方法接受三个参数,但是如果我们希望在方法调用期间仅传递两个参数,则 Java 编译器将使用第三个参数的默认值来避免任何编译错误。

Java 不支持默认参数值,但是我们可以使用一些内置解决方案来实现它,例如 var-args 或方法重载。让我们看一些例子。


Java 中的默认参数

在此示例中,我们使用方法重载方法来设置默认参数值。但是,这不是一个好的解决方案,但可以用作替代方案。注意,在调用 add() 方法时,我们将 0 作为默认值。

如果我们有两个相同类型的可选参数,则该方法无效,并且可以省略其中的任何一个。

public class SimpleTesting{
    int add(int a, int b) {
        return a+b;
    }
    int add(int a, int b, int c) {
        return a+b+c;
    }
    public static void main(String[] args) {
        SimpleTesting test =  new SimpleTesting();
        int val1 = 10;
        int val2 = 20;
        int result1 = test.add(val1, 0);
        int result2 = test.add(val1, 0, val2);
        System.out.println("resutl1 : "+ result1);
        System.out.println("resutl2 : "+result2);
    }
}

输出:

resutl1 : 10
resutl2 : 30

在 Java 中使用 var-args 设置默认参数

这是我们使用可变 args 功能设置默认参数的另一种方法。var-args 允许传递可变长度的相同类型的参数。请参见下面的示例。

public class SimpleTesting{
    int add(int a, int... b) {
        int b2 = b.length>0?b[0]:0;
        return a+b2;
    }
    int add(int a, int b, int c) {
        return a+b+c;
    }
    public static void main(String[] args) {
        SimpleTesting test =  new SimpleTesting();
        int val1 = 10;
        int val2 = 20;
        int result1 = test.add(val1);
        int result2 = test.add(val1, 0, val2);
        System.out.println("resutl1 : "+ result1);
        System.out.println("resutl2 : "+result2);
    }  
}

输出:

resutl1 : 10
resutl2 : 30

在 Java 中将默认参数设置为空字符串

对于字符串参数,我们可以为参数设置一个空字符串。但是,此字符串将 null 保留为默认值。请参见下面的示例。

public class SimpleTesting{
    String defaulPara(int a, String str1, String str2) {
        return str1+str2+a;
    }
    public static void main(String[] args) {
        SimpleTesting test =  new SimpleTesting();
        int val1 = 10;
        String result1 = test.defaulPara(val1,"","second");
        String result2 = test.defaulPara(val1,"first","");
        System.out.println("resutl1 : "+ result1);
        System.out.println("resutl2 : "+result2);
    }  
}

输出:

resutl1 : second10
resutl2 : first10

在 Java 中使用带有任意数量参数的 var-args 设置默认参数

在使用 var-args 的情况下,我们可以在调用方法时随意提供任意数量的参数。因此,如果你只想提供有限的参数,那么它将很好地工作。请参见下面的示例。

public class SimpleTesting{
    int defaulPara(int... a) {
        int sum = 0;
        for (int i : a) {
            sum+=i;
        }
        return sum;
    }
    public static void main(String[] args) {
        SimpleTesting test =  new SimpleTesting();
        int val1 = 10;
        int val2 = 20;
        int result1 = test.defaulPara();
        int result2 = test.defaulPara(val1);
        int result3 = test.defaulPara(val1, val2);
        System.out.println("resutl1 : "+ result1);
        System.out.println("resutl2 : "+result2);
        System.out.println("resutl3 : "+result3);
    }
}

输出:

resutl1 : 0
resutl2 : 10
resutl3 : 30

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

在 MySQL 中使用 CURRENT_TIMESTAMP 作为默认值

发布时间:2024/03/25 浏览次数:177 分类:MySQL

本文教你如何在低于 5.6.5 的 MySQL 版本中使用 CURRENT_TIMESTAMP 作为 DEFAULT。因此,你可以防止 MySQL 错误 1293。我们的方法包括重新排序表列、使用 DEFAULT 0 和时间值。

jQuery 获取 URL 参数

发布时间:2024/03/24 浏览次数:174 分类:JavaScript

在今天的文章中,我们将学习如何在 jQuery 中获取 URL 参数。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便