Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

private void function(Integer[] a, String str = "") like in PHP [duplicate]

Tags:

java

php

Possible Duplicate:
Does Java support default parameter values?

Is it possible to do something like this

private void function(Integer[] a, String str = "")

like in PHP. If I don't provide str, it will just be empty. In PHP it's possible, in JAVA it gives me error. Or the only solution here is to create two methods like this?

private void function(Integer[] a, String str)
private void function(Integer[] a)
like image 812
good_evening Avatar asked Aug 31 '12 14:08

good_evening


2 Answers

Exacly, there is no other option than:

private void function(Integer[] a, String str) {
    // ...
}

private void function(Integer[] a) {
    function(a, "");
}
like image 137
hsz Avatar answered Nov 09 '22 04:11

hsz


Declare your method with var agrs

private void function(Integer[] a, String... s)

Remember, var args should always be the last argument of the method.

like image 1
RP- Avatar answered Nov 09 '22 03:11

RP-