+ 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 Answers
+ 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.