+ 3
Why this not giving any compilation error?
Public static void main(String... p) { boolean val=false; if(val==true) System.out.println("true"); else System.out.println("false); }
5 ответов
+ 7
In JDK 5, Java has included a feature that simplifies the creation of methods that need to take a variable number of arguments. This feature is called varargs and it is short-form forvariable-length arguments. A method that takes a variable number ofarguments is a varargs method.
syntax:-
public static String format(String pattern, Object... arguments);
Varargs are useful for any method that needs to deal with an indeterminate number of objects. One good example is String.format. The format string can accept any number of parameters, so you need a mechanism to pass in any number of objects.
String.format("This is an integer: %d", myInt);
String.format("This is an integer: %d and a string: %s", myInt, myString);
that's why it is not give any compilation errors
+ 5
did you put that code in class?
+ 3
Why do you think it should?
+ 3
Yes i did
And output is false
+ 3
It's worth pointing out that behind the scenes varargs creates an array, making this equivalent to the 'normal' main declaration.