Does Java support Default Parameters?

0 votes

I came across some Java code that had the following structure:

public ParameterizedFunction(String param1, int param2)
{
    this(param1, param2, false);
}

public ParameterizedFunction(String param1, int param2, boolean param3)
{
    //use all three parameters here
}

I know that in C++ I can assign a parameter a default value. For example:

void ParameterizedFunction(String param1, int param2, bool param3=false);

Does Java support this syntax? 

May 22, 2018 in Java by sharth
• 3,320 points
176 views

3 answers to this question.

0 votes

No, the structure you found is how Java handles it.

Effective Java: Programming Language Guide's for constructors, Item 1 tip if the overloading is getting complicated.

For other methods, renaming some cases or using a parameter object can help

This happens when you have enough complexity such that  you cannot differentiate between them

 A definite case is where you have to differentiate using the order of parameters, not just number and type

answered May 22, 2018 by parth295
• 4,600 points
0 votes

Try this solution:

public int getScore(int score, Integer... bonus)
{
    if(bonus.length > 0)
    {
        return score + bonus[0];
    }
    return score;
}
answered Aug 16, 2018 by samarth295
• 2,190 points
0 votes

You can try this with method overloading.

void foo(String a, Integer b) {
    //...
}

void foo(String a) {
    foo(a, 0); // here, 0 is a default value for b
}

foo("a", 2);
foo("a");
answered Sep 21, 2018 by Sushmita
• 6,840 points

Related Questions In Java

0 votes
1 answer

does java support operator overloading?

Java doesn't supports operator overloading because it's just a ...READ MORE

answered Jun 18, 2018 in Java by Akrati
• 960 points
286 views
0 votes
1 answer

What are optional parameters in Java

Using three dots: public void move(Object... x) { ...READ MORE

answered Apr 27, 2018 in Java by developer_1
• 3,280 points
49 views
0 votes
1 answer

What does Java option -Xmx stand for?

-Xmxn: It specifies the maximum size, in bytes, ...READ MORE

answered May 9, 2018 in Java by sharth
• 3,320 points
195 views
0 votes
1 answer

How does omitting curly braces in Java program, effect the code ?

Using braces makes the code more maintainable ...READ MORE

answered May 14, 2018 in Java by Rishabh
• 3,540 points
33 views
+6 votes
11 answers

Default parameters of XMS and XMX in JVM

You use these Java command-line parameters to ...READ MORE

answered Nov 13, 2018 in Java by Murali
67,613 views
0 votes
1 answer

Executing commands with parameters in Java

Try using the below command: Runtime.getRuntime().exec(new String[]{"php","/var/www/script.php", "-m", ...READ MORE

answered Sep 5, 2018 in Java by geek.erkami
• 2,320 points
71 views
0 votes
1 answer
0 votes
0 answers

How can I use Selenium in the background?

I'm currently working on Selenium and I ...READ MORE

4 days ago in Selenium by Vaishnavi
• 1,080 points
10 views
0 votes
1 answer

Java Security: Illegal key size or default parameters

Starting from Java 9 or 8u151, you ...READ MORE

answered Aug 23, 2018 in Java by parth295
• 4,600 points
910 views
0 votes
2 answers

Does the finally block always execute in Java?

public static void main(String[] args) { ...READ MORE

answered Aug 8, 2018 in Java by Sushmita
• 6,840 points
228 views