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 Overflow
Discussions

What is the meaning of String[] args in public static void main(String[] args)?
It is an array of String arguments that are passed into the main method from the input of the application, so for example you can run a program using: java helloWorld.java And you can also input arguments to be used by the main method using: java helloWorld.java {string1} {string2} and then in the main method you can access these values using args[0], args[1] More on reddit.com
🌐 r/learnjava
25
82
March 7, 2021
Parameterized Strings in Java - Stack Overflow
Since Java 5, you can use String.format to parametrize Strings. More on stackoverflow.com
🌐 stackoverflow.com
how to add a constructor with a string parameter
So, if you don't then the constructor ... classas Java will use its own default constructor. Without this convention, the compiler won't know which method is a constructor and which are just instance methods so there needs to be a way of clearly defining that. ... Yes, that was immensely helpful. Thanks so much! ... You're pretty close. Both methods are constructors. One is a default one, which takes no parameters - you've done that correctly. Next, you want a constructor that takes a string ... More on teamtreehouse.com
🌐 teamtreehouse.com
2
September 8, 2015
clean code - Recommended value to pass instead of String parameter for a method in java - Software Engineering Stack Exchange
74 Is there a performance benefit to using the method reference syntax instead of lambda syntax in Java 8? ... 0 Is "the boolean value is from dynamic loaded data (eg:user input, database)" a reason to use boolean parameter? More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
July 11, 2016
🌐
W3Schools
w3schools.com › java › java_methods_param.asp
Java Method Parameters
Information can be passed to methods as a parameter. Parameters act as variables inside the method. Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.
🌐
Ibytecode
ibytecode.com › blog › passing-string-as-parameter-to-a-method
Passing String as parameter to a method – iByteCode Technologies
String objects are immutable in Java; a method that is passed a reference to a String object cannot change the original object. In main: Before Passing String to method: Hello In method(): Hello World In main: After returning from method: Hello
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
3 weeks ago - Parameters: format - A format string · args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited ...
🌐
University of Toronto
cs.toronto.edu › ~reid › web › javaparams.html
Java Method Arguments
A new is required, and Java does it behind the scenes. It creates a new object of class String and initializes it to contain the string literal we have given it. ... Because str is an object we might think that the string it contains might be changed when we pass str as a parameter to a method.
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java string › string interpolation in java
String Interpolation in Java | Baeldung
July 23, 2025 - @Test public void givenTwoString_thenInterpolateWithStringBuilder() { String EXPECTED_STRING = "String Interpolation in Java with some Java examples."; String first = "Interpolation"; String second = "Java"; StringBuilder builder = new StringBuilder(); builder.append("String ") .append(first) .append(" in ") .append(second) .append(" with some ") .append(second) .append(" examples."); String result = builder.toString(); assertEquals(EXPECTED_STRING, result); } As we can see in the above code example, we may interpolate the strings with the necessary text by chaining the append function, which accepts the parameter as a variable (in this case, two Strings).
🌐
Sololearn
sololearn.com › en › Discuss › 1935176 › why-java-method-takes-string-parameter-and-arg
why java method takes String parameter and arg | Sololearn: Learn to code for FREE!
public static void main(String ... the measures need to follow ... 'args' is an array of type String that allow you to run your java-code with some specific parameters out of the command line....
🌐
Baeldung
baeldung.com › home › java › java string › passing strings by reference in java
Passing Strings by Reference in Java | Baeldung
January 8, 2024 - Although there is a difference between passing a primitive or an object, variables or objects have their scope inside a method. In both cases, a call-by-sharing is what is happening, and we can’t directly update the original value or reference. Therefore, parameters are always copied. A String is a class instead of a primitive in Java.
🌐
GeeksforGeeks
geeksforgeeks.org › java › passing-strings-by-reference-in-java
Passing Strings By Reference in Java - GeeksforGeeks
May 14, 2023 - In Java, String objects are passed by reference, meaning that the reference to the memory location of the object is passed. However, due to the immutability of Strings, any changes made to the parameter within a method or function do not affect the original String object outside the method or function.
Top answer
1 of 3
9

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 attachDevice is 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)
{
    ...
    ...     
}
2 of 3
4

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.

🌐
W3Docs
w3docs.com › java
What is "String args[]"? parameter in main method Java
String args[] is a parameter in the main method of a Java program. It is an array of strings that can be used to pass command-line arguments to the program when it is executed.
🌐
Tibco
docs.tibco.com › pub › sfire-sfds › latest › doc › html › authoring › parameter-string-values.html
String Values in Parameters
Parameters are sometimes resolved in contexts where the parameter value is used as a literal string, such as an argument passed to a Java operator. On the other hand, many parameters are resolved in the context of an expression, such as in the Additional Expressions grid of a Map operator.
🌐
Kansas State University
textbooks.cs.ksu.edu › cc210 › 06-methods › 09-java › 02-parameters
Parameters :: CC 210 Textbook
June 27, 2024 - In Java, we can add parameters to a method declaration by placing them in the parentheses () at the end of the declaration. Each parameter is similar to a variable declaration, requiring both a type and an identifier. We can also define multiple parameters, separated by commas ,. For example, ...
🌐
Quora
quora.com › Why-is-a-string-parameter-compulsory-in-main-method-in-java
Why is a string parameter compulsory in main method in java? - Quora
Answer (1 of 3): Let us first understand why we need the main method at all. The main method is the entry point for any java application. When we run a compiled java class using the ‘Java’ command, the JVM looks for the main method in the class and starts by calling it.
🌐
Quora
quora.com › In-Java-are-string-variables-pass-by-reference-or-pass-by-value
In Java, are string variables pass-by-reference or pass-by-value? - Quora
... M.C.S. · Author has 2.3K answers ... short, int, long, float, double) are always passed by value. Strings are objects and are therefore passed by reference....