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 23, 2018 in Java by sharth
• 3,370 points
8,127 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 23, 2018 by Parth
• 4,640 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 17, 2018 by samarth295
• 2,220 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,920 points

Related Questions In Java

0 votes
1 answer

does java support operator overloading?

In some cases, like the following, operator ...READ MORE

answered Dec 18, 2021 in Java by Christoph S.

edited Mar 5, 2025 42,953 views
0 votes
1 answer

Why Java does not support multiple inheritance?

Java does not support multiple inheritance through ...READ MORE

answered Oct 23, 2023 in Java by anonymous
• 3,360 points
1,131 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,350 points
1,918 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,370 points
2,499 views
+11 votes
13 answers

What are the default xmx and xms values ?

Run this code to see all the ...READ MORE

answered Nov 13, 2018 in Java by anonymous
411,904 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,680 points
3,875 views
0 votes
1 answer
0 votes
1 answer

How can I use Selenium in the background?

Hey there! I would suggest you try using ...READ MORE

answered Jul 22, 2019 in Selenium by anonymous
7,903 views
0 votes
2 answers

Java Security: Illegal key size or default parameters

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

answered Aug 23, 2018 in Java by Parth
• 4,640 points
9,590 views
0 votes
2 answers

Does the finally block always execute in Java?

If there is System.exit() present in the ...READ MORE

answered May 28, 2022 in Java by Abhishek

edited Mar 5, 2025 3,074 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP