0

I came across a question in Java interview where he asked me: How to call a function say add(int a, int b, boolean value) with only passing two arguments like add(int a, int b). I was not aware about the same, and ask him for some hint then he replied me:

Go and read the following concept

Integer a;

int a;

Now my question is How the hint is related with the question and how can we achieve the same.

Thanks in advance

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
Ajay Yadav
  • 31
  • 1
  • 1
  • 7
  • 2
    Without providing a two-argument overload which invokes the three-argument overload (e.g. using `true` as the value for the 3rd parameter), you can't. – Andy Turner Mar 14 '17 at 14:00
  • 1
    "How the hint is related with the question" Absolutely no idea. The hint that you've given doesn't make sense. – Andy Turner Mar 14 '17 at 14:02
  • He asks something impossible, imply that he expects a creative answer. .... I will answer "My real-life solution for this problem is to use C++ instead of Java. (default parameter) If you hire me, I will revolutionize your company to use C++ instead." – javaLover Mar 14 '17 at 14:04
  • You can go with void add(Object... parameters) :-) – Chris311 Mar 14 '17 at 14:07
  • @javaLover: why C++, use Kotlin – P.J.Meisch Mar 14 '17 at 14:21
  • Possible duplicate of [Java optional parameters](http://stackoverflow.com/questions/965690/java-optional-parameters) – AxelH Mar 15 '17 at 06:59

5 Answers5

1

You just cant break the methods signature, you will need to Overload the method and reduce/remove the paramters you just dont need/want

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
1

I think you can do it by using the wrapper and boxing concepts

For example in your case you can create another method to receive two parameters and then do something like this:

add(int a, int b){
    Boolean c = null;
    add(a, b, c);
}

Or with an int value:

add(int a, boolean c){
    Integer b = null;
    add(a, b, c);
}

In that way you can change the primitive to its wrapper class... however, in the call it will throw and exception...

The reason is the auto-unboxing of the Integer b = null into a int. To unbox, the JVM will call int Integer.intValue() on a null value. To correct this exception, the solution is to prevent any unboxing, so the method needs to work with the wrappers classes. Here is the method :

add(Integer a, Integer b, Boolean c){
    //...
}

Since the parameter are not primitive, there is no need to try unboxing the null value. But this is needed inside of the method like any Object parameter. You still need to do some null check before using any nullable value. So you can't expect a + b to return something if b is null.

AxelH
  • 14,325
  • 2
  • 25
  • 55
Dazak
  • 1,011
  • 2
  • 9
  • 17
  • This will throw a NPE on each call since the JVM will tried to call `Boolean.booleanValue()` to get the primitive form on a null instance. (Same with the Integer). – AxelH Mar 14 '17 at 14:18
  • Well... maybe is not so useless at all... It could works as a bad example ;) – Dazak Mar 14 '17 at 19:48
  • Don't, just end with the method signature using the wrapper type to be usable ;) with explanation – AxelH Mar 14 '17 at 19:48
  • @AxelH I will appreciate if you help me to improve this answer ;) – Dazak Mar 14 '17 at 19:53
  • here is the idea. I let you see what you can do with the explanation ;) – AxelH Mar 15 '17 at 06:53
0

Default parameter don't exist in java, but you can reproduce this easily with overloading.

public int add(int a, int b){
    return this.add(a, b, true);
}

public int add(int a, int b, boolean value){
    return value ? ( a + b) : (a - b);
}

This boolean is "optionnal" with a default value of true.

So this can be use like:

add(1, 2);           // 3
add(1, 2, false);    // -1

This seems to be the same method but this is actually an overloading, one calling an other.

AxelH
  • 14,325
  • 2
  • 25
  • 55
0

You can use Varargs, but you need to change the position of the boolean.

private void add(boolean a, int ... b){

}

You can invoke it like this

add(true, 1, 2);
add(false, 1);
add(true);

The varargs are optional and the data type is an array of the type.

If you need to use different types, you can use an array of objects. Object ... b.

Or you can define the array directly

private void add(int[] b, boolean a){

}

In this case you always have 2 params.

a = {1, 2};
add(a, true);
reos
  • 8,766
  • 6
  • 28
  • 34
  • I like this solution, I use it a lot when I have to many optionnal (5+ overloading is messy) but this is restrictive on type (or need casting) – AxelH Mar 14 '17 at 14:25
0

It seems to be like this case: Java optional parameters

The fourth post explains each solution you can do.

Community
  • 1
  • 1