I would like to wrap the String.format() method with in my own Logger class. I can’t figure a way how to pass arguments from my method to String.format().
public class Logger
{
public static void format(String format, Object... args)
{
print(String.format(format, args)); // <-- this gives an error obviously.
}
public static void print(String s)
{
System.out.println(s);
}
}
Your code works. The vararg is more or less simply a syntactic boxing of the vararg.
In other words,the following two statements are actually identical:
Your
argsin your code will always be anObject[], no matter if you have 0, 1, 2 or any other number of arguments.Note that this is determined at compile time and looks at the static type of the object, so
String.format("%s %s", (Object)new Object[] {"Foo", "Bar"})will cause the array to be treated as a single object (and in this case cause a runtime error to be thrown).If you still have problems with your code, please check that your example really is identical to how your code works.