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,310 points

3 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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);
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
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
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,310 points
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,520 points
+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
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
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,360 points

edited Nov 6, 2018 by Omkar 5,222 views
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,050 points
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
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

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.