21

Android function

PHP example:

function HaHa($a = "Test")
{
    print $a; 
}

The question is how to do it in android...

public void someFunction(int ttt = 5)
{
   // something
}

The solution above doesn't work, how can I do it?

Thanks!

Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
M.V.
  • 1,662
  • 8
  • 32
  • 55
  • I don't think we can do that in Java. and I just can make a wild guess, if I remember correctly, that this is a feature that is supposed to be in Java 7. So in this case, one can check the variable `ttt` for being null, and if its null, assign a value to it. – Aman Alam Apr 07 '11 at 12:55
  • 1
    Related: [Does Java support default parameter values?](http://stackoverflow.com/questions/997482/does-java-support-default-parameter-values) – eldarerathis Apr 07 '11 at 12:57
  • Possible duplicate of [Java optional parameters](http://stackoverflow.com/questions/965690/java-optional-parameters) – T.Todua Jan 12 '17 at 08:29

7 Answers7

15

No, Java does not support default values for function parameteres. There's an interesting post about borrowing language features here: http://java.dzone.com/news/default-argument-values-java

Bendihossan
  • 2,407
  • 5
  • 22
  • 25
  • I thought so but native functions support it somehow... I can't actually remember one... Or for example JSONArray can be built from different sources: String, JSONTokener x, Collection collection or blank... I thought there could be work around – M.V. Apr 07 '11 at 13:02
  • Well work around for me is to put String[] inside and can add or remove arguments... – M.V. Apr 07 '11 at 13:03
14

No need to overload anything, just write:

public int getScore(int score, Integer... bonus)
{
    if(bonus.length > 0)
    {
        return score + bonus[0];
    }
    else
    {
        return score;
    }
}
Hamzeh Soboh
  • 7,572
  • 5
  • 43
  • 54
13

You can abuse overloading like this:

int someMethod() { return someMethod(42); }
int someMethod(int arg) { .... }
Ingo
  • 36,037
  • 5
  • 53
  • 100
5

you can use 3 dots syntax:

public void doSomething(boolean... arg1) {
    boolean MyArg1= (arg1.length >= 1) ? arg1 : false;
}
T.Todua
  • 53,146
  • 19
  • 236
  • 237
1

You can use overloading a function . The overloading is also okay but in case you need default values for multiple arguments you will end up creating having many methods with all possible combination of default arguments, for the example you use imagine you want to have a default value for the 3 arguments. you will end up with this

public void methodA(A arg1) {  }
public void methodA( B arg2,) {  }
public void methodA(C arg3) {  }
public void methodA(A arg1, B arg2) {  }
public void methodA(A arg1, C arg3) {  }
public void methodA( B arg2, C arg3) {  }
public void methodA(A arg1, B arg2, C arg3) {  }

So here's the hack i did for me , you can also use

public static void main(String[] args)
{
    defaultParameter();
    defaultParameter(true);
}

public static void defaultParameter(Boolean ...gender)
{
    boolean genderBoolean  = false; // It the default value you want to give
    if(gender.length == 1)
    {
        genderBoolean = gender[0];  // Overrided Value
    }
    System.out.println(genderBoolean);
}

The above code will genrate result

false
true

I find out here the example java-default-parameter-values

Jigar Shah
  • 69
  • 1
  • 2
1

You can do it now in Android by using Kotlin! Kotlin supports default arguments, so you can write a function like:

fun addTheBook(title: String, author : String = "No Name"){...}

And then you can call it just like:

addTheBook("The Road Less Traveled")
Marija
  • 422
  • 4
  • 12
0

Java doesn't support a syntax that will do what you want.

Perhaps at the start of someFunction(int), you could just check the incoming value, and assign a different value if you don't like what's coming in.

if (ttt == 0) ttt = 5;

Please note this question appears to have nothing to do with android, and so is improperly tagged.

Thane Anthem
  • 4,093
  • 4
  • 26
  • 24