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
6,941 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?

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

answered Jun 18, 2018 in Java by Akrati
• 960 points
41,739 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
447 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
956 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
1,718 views
+11 votes
13 answers

Default parameters of XMS and XMX in JVM

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

answered Nov 13, 2018 in Java by anonymous
402,703 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,128 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,124 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
8,627 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,920 points
2,062 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