0

After browsing the web for a while looking for an answer to my question I can't find any posts which seem to offer an effective solution to my issue.

Currently I would overload a method for each different data type input.

For example:

public void ex1(String in){
public void ex1(int in){

I would imagine there would be a way to condense this into one line

public void ex1(opt(String in), opt(int in)){

or

public void ex1((String/int) in){

However, as far as I've seen, nobody has presented a reasonable method to use either of these forms.

Overloading does work, but doesn't look as clean as I might like it, so any workarounds or ideas would be appreciated.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Skad
  • 5
  • 1
  • 2
    Use a different language? Imagining something won't change Java. – Dave Newton Nov 26 '15 at 02:25
  • Just to clarify, you're suggesting I stick with overloading as the most effective way to do this? – Skad Nov 26 '15 at 02:27
  • 1
    As an example, look at all the overloadings in the `Arrays` class. https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html If there was a better way, they'd have used it, but there isn't. – Paul Boddington Nov 26 '15 at 02:31

4 Answers4

1

There is a way, but you acn only provide one optional parameter. That way is to use ... in front of parameter type.

For ex:

public void ex1(int someint, String... in)){

In this case String in is an optional parameter which you can provide or not. But int someint is a must provide parameter.

So String... in is basically a String[] in. Since array can be null.... Other than this (atleast I) dont know of any other way to achieve this.

This means you also cant do something like one of the parameters has to be entered and other can be ignored. You can ignore just one parameter and other has to be passed.

Also this works with just one parameter per method. In other words, just one parameter can be made optional. Else you must use method overloading.

However (as said in comments), this is not an effective way. Since you will have to write logic for each varaible's possibility and the code would be a great mess, compared to the sweet and effective way by method overloading.

EDIT:

varargs (or this optional parameter) must appear as the last parameter in method. Thanks @Makoto for pointing this out.

Jaskaranbir Singh
  • 2,034
  • 3
  • 17
  • 33
  • Then you'd have to check for presence and mish-mash all the logic in a single method, which doesn't seem any better than just checking the type. – Dave Newton Nov 26 '15 at 02:29
  • Yup (I believe OP would have considered that even before asking the question), and that is the whole reason I do not encourage this. Java has method overloading like goodies for a reason afterall :) – Jaskaranbir Singh Nov 26 '15 at 02:31
  • Marking this as answer because, even though I can't use your provided solution, you clarified some restrictions I had to follow, and will be continuing with method overloading as I have to, for this example at least, include 4 different primitive data types. It seems that there may be workarounds, provided by others posting here, however they delve into techniques I'm not familiar with and/or involve complication such that it negates the original purpose, making it look cleaner – Skad Nov 26 '15 at 02:42
  • Sorry about that, those were silly mistakes (even though I said that my self in a comment that two params cannot have same name lol). Anyways, fixed, thanks for pointing out :) – Jaskaranbir Singh Nov 26 '15 at 02:47
0

I would imagine there would be a way to condense this into one line

You could probably do something like this:

public void ex1(String in1, int in2) { // I don't believe they can have the same name
  // ...
}

As far as an optional variables, I would just set the variables to a value, and inside the method, test to see if that value is equal to that "default" value.

ex1("Hey", 0);

public void ex1(String in1, int in2) {

  boolean useString = true;
  boolean useInt = true;

  if(in1.length() < 1) {
    useString = false;
  }
  if(in2 != 0) {
    useInt = false;
  }

  if(useString) {
    // ...
  }
  if(useInt) {
    // ...
  }

}

EDIT: For one optional variable, see Jaskaranbir Singh's answer.

Rane1011
  • 543
  • 4
  • 7
  • you cant specify same names for two variables even as parameters in method. – Jaskaranbir Singh Nov 26 '15 at 02:34
  • The idea of my method is that the method would only take in one value, it seems that in your example it would take in a value for each parameter, even if it wasn't intended to be used. In my case I will be using 4 different data types, and don't think ex1(null, null, 3, null) is an improvement. Also, it's starting to get to the point where method overloading is simpler regardless. – Skad Nov 26 '15 at 02:39
-1

Try this:

public void ex1(Serializable in){
// .....
}

Always works for me. String is Serializable, and for primitive types it use autoboxing and convert automatically int to Integer and so on, and Integer is also Serializable, so ex1(1) will work as expexted. All you need is detect real type:

if(Integer.class.isInstance(in)){
int intVal = ((Integer)in).intValue();
// ...
} else if(String.class.isInstance(in)){
String strVal = (String) in;
// ...
}

Corner case is when in is null, - in that case its hard to detect real type(still possible), but most of all you dont need that.

msangel
  • 9,895
  • 3
  • 50
  • 69
  • 1
    This doesn't feel any cleaner than creating multiple methods to deal with those types. In essence, you're replacing overloading which has been an accepted paradigm for years with using `isInstance`/`instanceof`, which is best to avoid if you can help it. – Makoto Nov 26 '15 at 02:47
-2

what I would do is this:

public void ex1(String[] args){}

by using this you can have as many data types as you want, if any.

The full code would look something like this:

public void ex1(String[] args){
   try{
      if(/*I know there is some parse string techique*/args[] == ...){
         //code here
      }
   catch(Exception E){}

hopefully you get the idea, basically you have an array of string an parse it into an int, boolean, ect...and put the code that you would put in the separate functions into the if statements