Reputation: 219
I am would like to know what the String...
keyword is. From my understanding it is a pseudonym for String[]
. However it does not seem to behave the same way. For example I have a server that will send the string "<PING>\n"
to a client. The client processes this message with the method:
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
//in the arrayList we add the messaged received from server
arrayList.add(values[0]);
// notify the adapter that the data set has changed. This means that new message received
// from server was added to the list
mAdapter.notifyDataSetChanged();
parseMessage(values);
}
Then for debugging purposes I print to console the items in values in parseMessage()
:
private void parseMessage(String... theMessage)
{
System.out.println("-----------------------------");
for (int i=0; i<theMessage.length; i++)
{
System.out.println(theMessage[i]);
}
System.out.println("-----------------------------");
if (theMessage[0].equals("<PING>"))
{
mTcpClient.sendMessage("<PING>");
}
else if (theMessage[0].equals("<PING>\n"))
{
mTcpClient.sendMessage("<PING>");
}
else
{
mTcpClient.sendMessage(theMessage[0]);
}
}
This gives me the expected output on the first message I receive:
02-07 14:39:51.277: I/System.out(10823): -----------------------------
02-07 14:39:51.277: I/System.out(10823): <PING>
02-07 14:39:51.277: I/System.out(10823): -----------------------------
However the second time the client recieves "<PING>\n"
the console displays:
02-07 14:40:01.298: I/System.out(10823): -----------------------------
02-07 14:40:01.298: I/System.out(10823): ��<PING>
02-07 14:40:01.298: I/System.out(10823): -----------------------------
I do not understand why it displays the symbols �
. The second time it receives "<PING>\n"
I expect it to display:
-----------------------------
<PING>
<PING>
-----------------------------
Can someone please explain the discrepancy between the expected output and actual output?
Upvotes: 1
Views: 158
Reputation: 199215
The ...
in general stands for variable number of arguments
So you can have a method with variable number of arguments of any type.
The strange symbol you're seeing in your output has nothing to do with this. You may try to see what's inside with Arrays.toString() method:
$ cat A.java
import java.util.Arrays;
public class A {
public void example( String ... args ) {
System.out.println( Arrays.toString( args ) );
}
public static void main( String ... args ) {
A a = new A();
a.example("ping", "pong\n");
a.example("That's", "all", "folks");
}
}
$ java A
[ping, pong
]
[That's, all, folks]
Upvotes: 2
Reputation: 16351
String...
is indeed a syntaxic sugar that replaces String[]
and allows you to pass a undefined numbers of arguments to a method.
Example :
public void withArray(String[] values){ ... }
withArray(new String[] {"a", "b"});
public void withVarArgs(String... values) { ... }
withVarArgs("a", "b");
Variable args allow you to pass arguments directly, skipping new String[]
declaration.
Upvotes: 1
Reputation: 9582
When you call the function, it turns the list of comma separated arguments into an array of strings.
Upvotes: 2
Reputation: 137282
...
is a variable argument list.
Inside the function it may be treated as an array.
Upvotes: 4