+ 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 Antworten
+ 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