It's called varargs; http://docs.oracle.com/javase/6/docs/technotes/guides/language/varargs.html
It means you can pass an arbitrary number of arguments to the method (even zero).
In the method, the arguments will automatically be put in an array of the specified type, that you use to access the individual arguments.
Answer from dagge on Stack OverflowIt's called varargs; http://docs.oracle.com/javase/6/docs/technotes/guides/language/varargs.html
It means you can pass an arbitrary number of arguments to the method (even zero).
In the method, the arguments will automatically be put in an array of the specified type, that you use to access the individual arguments.
Yes, that means you can take arbitrary no of Strings as an argument for this method.
For your method:
public void method(String... strs);
You can call it as:
method(str)
method(str1, str2)
method(str1,str2,str3)
Any no of arguments would work. In other words, it is a replacement for:
public void method(String[] str);
(String ...) is an array of parameters of type String, where as String[] is a single parameter.
Now here String[] can full fill the same purpose here but (String ...) provides more readability and easiness to use.
It also provides an option that we can pass multiple array of String rather than a single one using String[].
A feature of String[] vs String... is that the "String..." does not need to be part of the call.
public void test(String... args){
if(args.length > 0){
for( String text : args){
System.out.println(text);
}
}else{
System.out.println("No args defined.");
}
}
public void callerTest(){
test();
System.out.println();
test("tree");
System.out.println();
test("dog", "cat", "pigeon");
}
Then if you call callerTest(); the Output will be:
No args defined.
tree
dog
cat
pigeon
What is the meaning of String[] args in public static void main(String[] args)?
Parameterized Strings in Java - Stack Overflow
how to add a constructor with a string parameter
clean code - Recommended value to pass instead of String parameter for a method in java - Software Engineering Stack Exchange
Videos
This seems to be always included in java's main method, but I still don't understand what it means honestly.
Edit: thanks!
String.format()
Since Java 5, you can use String.format to parametrize Strings. Example:
String fs;
fs = String.format("The value of the float " +
"variable is %f, while " +
"the value of the " +
"integer variable is %d, " +
" and the string is %s",
floatVar, intVar, stringVar);
See http://docs.oracle.com/javase/tutorial/java/data/strings.html
Alternatively, you could just create a wrapper for the String to do something more fancy.
MessageFormat
Per the comment by Max and answer by Affe, you can localize your parameterized String with the MessageFormat class.
You could use String.format. Something like:
String message = String.format("You requested %2$s but were assigned %1$s", "foo", "bar");
will generate
"You requested bar but were assigned foo"
I would discourage you to ever use null since it can lead to a further NPE, which are hard to debug (and cost a lot if they occur in production code).
Solution 1 (overload method)
If no deviceName is provided, you can provide a default one instead. The biggest disadvantage from this approach is the danger in genericDeviceMap.put(deviceName, device) because it can silently override the entry whose key is the default name (therefore, losing track of the previous Device).
public void attachDevice(Device device)
{
attachDevice(device, "DefaultName");
}
public void attachDevice(Device device, String deviceName)
{
..
device.setName(deviceName);
genericDeviceMap.put(deviceName, device);
..
}
Solution 2 (extract method)
Maybe that with your current architecture it doesn't make sense to add an entry to genericDeviceMap when attachDevice is called without a name. If so, a good approach is to only extract the common behaviour between the two attachDevice into private methods. I personnally don't like this approach for 2 reasons:
- The behaviour between the two
attachDeviceis not the same, one has a side-effect (device.setName(deviceName)) and the other not - The side-effect in itself who often lead to subtle bugs because you alter an object who's coming from an outside scope
Code:
public void attachDevice(Device device)
{
preAttachDevice();
postAttachDevice();
}
public void attachDevice(Device device, String deviceName)
{
preAttachDevice();
device.setName(deviceName);
genericDeviceMap.put(deviceName, device);
postAttachDevice();
}
private void preAttachDevice()
{
...
}
private void postAttachDevice()
{
...
}
Solution 3 (remove method)
My favorite, but the hardest. Ask yourself if you really need these two methods ? Does it make really sense to be able to call attachDevice either with a name or not ? Shouldn't you be able to say that attachDevice must be called with a name ?
In this case the code is simplified to only one method
public void attachDevice(Device device, String deviceName)
{
..
device.setName(deviceName);
genericDeviceMap.put(deviceName, device);
..
}
Or on the other hand, do you really need to maintain a Map of devices and devices names and set the device's name ? If not, you can get rid of the second method and only keep the first one.
public void attachDevice(Device device)
{
...
...
}
I'd argue that your team leader is asking you to create bad code. Having to pass some arbitrary value in as a name parameter when you do not want to specify a name is messy and confusing for any devs looking to use the method.
Instead, keep your two methods (though I'd rename your latter one to something like attachNamedDevice to make it clear it does something different to attachDevice, then move the common code into private methods:
public void attachDevice(Device device)
{
preDeviceNameSetup(device);
postDeviceNameSetup(device);
}
public void attachNamedDevice(Device device, String deviceName)
{
preDeviceNameSetup(device);
device.setName(deviceName);
genericDeviceMap.put(deviceName, device);
postDeviceNameSetup(device);
}
private void preDeviceNameSetup(Device device)
{
...
}
private void postDeviceNameSetup(Device device)
{
...
}
That way, you keep the API clean, but avoid code duplication in the implementation.