Re: print("my name is {name}, and {age}-year old {gender}", name, age, gender);



Mike,

This feature (varargs) makes printf possible without this kind of code:

printf("the format goes %s here",new Object[]{"stuff"});

It makes the reflection APIs more comfortable (I don't use them
anyway).

It makes Arrays.asList more readable:

List<String> strings=Arrays.asList("blah","blih","bloh");

It has merit in terms of readability, but no, it is not absolutely
required. It has a drawback too, from the called method's point of
view, the arguments passed in might be of zero length, or might even be
null, which is extra stuff to check for.

int varArgMethod(int... numbers)
{
for (final int i: numbers)
System.out.println(i);
}

This code could throw a NullPointerException (I think).

Chris Smith should note the absence of a link to a certain article.

.



Relevant Pages

  • Re: maths for programming C++
    ... To paraphrase someone else, if correctness isn't a requirement, then I'll ... int main ... printf x); ... So, under the rules of readability over correctness, the second ...
    (comp.programming)
  • Re: Can somebody help...
    ... a number of unsafe constructs and non-standard extensions. ... That value is too large to portably store in an int. ... This results in undefined behavior because you lied to printf about what ...
    (comp.lang.cpp)
  • Re: Is this correct..??
    ... long int i; ... Almost everthing is defined in assembler. ... Easily defeated by compiler optimisations. ... printf and that the first assignment to i is never used and decides to ...
    (comp.lang.c)
  • Semaphores Block when they Shouldnt and Dont when they Should
    ... took the votes (encoded in the first character of the transmitted mes- ... int socketAccess; ... write(voteSocket, voteBuffer, 2); ... {printf("Couldn't make a socket.\n"); ...
    (comp.unix.programmer)
  • Re: Linux / NASM equivalent of Iczelions Win32 assembly tuts
    ... int main ... putstring; ... care about code portability from toolset to toolset. ... readability, portability, flexibility... ...
    (alt.lang.asm)