This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
You can't have a variable return the way it works with variable parameters.
You could use a List or an Array though.
JDBCSupport - An easy to use, light-weight JDBC framework -
Joined: Jun 05, 2009
Technically, String... Parameter is a String Parameter with a number of elements in the array equals to the number of passed arguments and thus when I pass the Parameter argument, it is passing an array to the script engine but it gives a warning that I don't like. Basically if the script language was expecting a string but got a String array, that would be bad. It is unfortunate that I cannot pass a variable number of arguments; really, really unfortunate.
Oficial request to Java developers, please provide some way to pass variable number of arguments to a method.
Joined: Feb 23, 2009
I'm having problems following.
A method defined as void method(String... varArgs) can take an x-amount of Strings which are then shredded into an Array object. How, if not in some sort of Array, List or Collections would you iterate over arguments which length is not programatically defined ?
If you have a method that takes a String... and you pass a String, that shouldn't give any problems.
Now, if you have a method that takes an Object... and you pass a String, that String can be treated in two ways:
- as a single element, because String is a subtype of Object
- as an Object, because String is a subtype of Object
Now you're getting the warning because the compiler can't really determine which one you want; I believe it will treat the String as an Object. To prevent this error, cast the String as Object:
jake benn wrote:but it gives a warning that I don't like.
That's a good thing. You should prevent all warnings. After all, there is a reason for the warning. In this case, the warning informs you that the behaviour may not be what you expect.
actually I have a method that takes a variable number of strings.
It looks like this:
So, if I am doing this:
the Object is the class and is defigned with an object at the bottom of the Ruby file. The functionName is the name of the function I am calling. In this case it is, "var_length_args". The problem comes when I have a variable number of arguements sitting in my Java method and I need to pass all of those through to the Ruby method. Passing Ruby the string array is a solution but one I have to implement on the Ruby side. I was hoping for a solution on the Java side. Whatever, I guess in the end it is sixes.