在Java中,是否可以声明String类型的默认参数?

32

在使用String方法中,是否可以使用默认参数?以下是代码:

public void test(String name="exampleText") {
}

上面的代码出现了错误。是否有可能进行修正?


2
不,这在Java中是不可能的。 - Rohit Jain
5
通常可以使用方法重载来解决这个问题。 - Sotirios Delimanolis
你想要实现什么? - Braj
请查看此链接:https://dev59.com/jHNA5IYBdhLWcg3wWsYA#47441378 - simhumileco
2个回答

62
不,通常的做法是重载该方法,例如:
public void test()
{
    test("exampleText");
}

public void test(String name)
{

}

16

不,它不是。然而,以下是可能的:

public void test() {
    test("exampleText");
}
public void test(String name) {
    //logic here
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接