5

I am writing a java function which which takes two argument : one is a string type and other could be an array of String, Integer, Float, Double, Boolean or just a String.

In JavaScript it is quite easy to pass any data as an argument, but what are the good solutions in java ?

5
  • 3
    Use the power of generics Commented Apr 20, 2015 at 6:10
  • 4
    It's poor Object Oriented design to have method that handles multiple data types. What are you going to do with data? You can think about property you interested in and pass Interface as parameter. Commented Apr 20, 2015 at 6:15
  • 1
    We need to see more code to give a good recommendation. Usually, you can avoid having to do something like this. Without more context, I'd say look at "method overloading" (like System.out.println does it). But again, without more context, any recommendation is just a dangerous shot in the dark. Commented Apr 20, 2015 at 6:15
  • I am creating a java API which run SQL queries. so some data fetching methods requires to take two argument, first one is name of the column and second one is to list of values that must present in that column. SELECT * FROM table_name WHERE column_name IN(data1, data2, data3) Commented Apr 20, 2015 at 6:31
  • For this scenario, an overloaded set of methods seems to make sense. However, before setting out to write Java code to build SQL queries, consider taking a look at existing solutions (like jOOQ). Commented Apr 20, 2015 at 7:48

5 Answers 5

2

You can overload the method to accept different parameters.

 void doSomething(String x, int[] y) {}

 void doSomething(String x, String[] y) {}

 void doSomething(String x, String y) {}

That way, you still have type-safety (which an Object argument does not give you).

Sign up to request clarification or add additional context in comments.

2 Comments

How would this be superior to generics? Considering Java uses strong type checking for generic code.
@b1nary.atr0phy: Not sure what you mean. How would you use generics here?
2

In java you can only pass the data defined in by the method. A way around this can be to use Object as an argument. Besides that you can create many methods with the same name, but accepting different arguments. Example:

public void example(String str, Object obj){ /* code */ }

When using Object you need to make sure to check what type it is, so you don't end up trying to use an integer as a string array.

I am quite sure that using Object is not the best way to go and you should use multiple methods with different arguments.

1 Comment

This is a very bad habit to get into, as it violates type safety.
0

I suppose this is what you want

public void method1(String str, Object ...val)

Inside the method body, you will have to handle different objects accordingly

Comments

0

The prototype of method as per your need is as follows :

public void testMethod(String strVal, Object[] genericVal)

Now in the body part you have to cast the object into the required format by Using Wrapper Classes.

Above implementation will resolve the issue.

Comments

0

As you want second object as any data type array you can do something like this

public void _methodName(String arg1,Object[] arg2)

As String,Integer,Float,Boolean all are inherited from java.lang.Object,you can simply pass Object[] as second argument and you pass argument(String,Int,....etc) whatever you want.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.