+ 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); }

30th Dec 2017, 9:55 AM
Nikhil Gaurav
Nikhil Gaurav - avatar
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
30th Dec 2017, 10:26 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 5
did you put that code in class?
30th Dec 2017, 10:02 AM
Vukan
Vukan - avatar
+ 3
Why do you think it should?
30th Dec 2017, 10:00 AM
Dan Walker
Dan Walker - avatar
+ 3
Yes i did And output is false
30th Dec 2017, 10:08 AM
Nikhil Gaurav
Nikhil Gaurav - avatar
+ 3
It's worth pointing out that behind the scenes varargs creates an array, making this equivalent to the 'normal' main declaration.
30th Dec 2017, 10:33 AM
Dan Walker
Dan Walker - avatar