|
Sharkysoft home | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--lava.clib.stdarg.Va_list
Syntactic sugar for variable argument lists.
Details: Va_list
is an adaptation of the va_list
array variable used in C programs that implement variable argument lists. (These programs often include the stdarg.h header file.) In this Java port, Va_list
is made available as a convenient interface for building variable argument lists, for use in functions that require them, such as Stdio.printf
.
Since Java does not support variable-length and untyped argument lists, variable argument lists must be passed as Object
arrays. This class exists solely to aid in the inline construction of such Object arrays.
The following three examples demonstrate how this class can be used in conjunction with the printf
methods. Each example produces exactly the same output.
Example 1: Calling printf
using Va_list
(easy):
String name; int age; Stdio.printf ( "My friend %s is %d years old.\n", new Va_list () . add (name) . add (age) );
Example 2: Calling printf
using Va_list
(more efficient):
Stdio.printf ( "My friend %s is %d years old.\n", new Va_list (2) . add (name) . add (age) );
Example 3: Calling printf
without using Va_list
(most efficient):
Stdio.printf ( "My friend %s is %d years old.\n", new Object[] { name, new Integer (age) } );
Although using Va_list
to produce Object
arrays can in some cases produce more readable code, in general it is not as efficient as other Object
array construction techniques. Nevertheless, this class is available for those who are squeamish about constructing Object
arrays on their own.
Compatibility note: Because of changes made by Sun to java.util.Vector in Java 1.2, this class can no longer inherit from Vector. This modification should not affect most programs that use this class, but some programs may require recompiling.
Constructor Summary | |
Va_list()
Initializes a new instance whose final length is undetermined. |
|
Va_list(int length)
Initializes a new instance whose final length is known ahead of time. |
Method Summary | |
Va_list |
add(char c)
Appends a char to the list. |
Va_list |
add(double d)
Appends a double to the list. |
Va_list |
add(float f)
Appends a float to the list. |
Va_list |
add(int i)
Appends an int to the list. |
Va_list |
add(long l)
Appends a long to the list. |
Va_list |
add(java.lang.Object o)
Appends an Object to the list. |
Va_list |
add(short s)
Appends a short to the list. |
java.lang.Object[] |
done()
Converts the list to an Object array. |
Methods inherited from class java.lang.Object |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait |
Constructor Detail |
public Va_list()
public Va_list(int length)
length
- intended length of argument listMethod Detail |
public Va_list add(java.lang.Object o)
String
s, to the list.)o
- object to be addedpublic Va_list add(char c)
public Va_list add(short s)
public Va_list add(int i)
public Va_list add(long l)
public Va_list add(float f)
public Va_list add(double d)
public java.lang.Object[] done()
|
Sharkysoft home | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |