We must create an array variable to hold the data that you want to pass as an argument to a method otherwise you will get a compile time error.
Eg:
public class PassingArrayToMethodDemo {
static void methodA(int a[]){
//some statements to accomplish the task here
}
public static void main(String[] args) {
methodA({1,3,4,5}); // gives compile time error
int arr[] = {1,3,4,5};
methodA(arr); //it is legal to pass an array variable
}
}
Eg:
public class PassingArrayToMethodDemo {
static void methodA(int a[]){
//some statements to accomplish the task here
}
public static void main(String[] args) {
methodA({1,3,4,5}); // gives compile time error
int arr[] = {1,3,4,5};
methodA(arr); //it is legal to pass an array variable
}
}
No comments:
Post a Comment