I have absolutely no idea as to why this code won’t return an array… I feel like there is a problem with my compiler:
public class trial1{
public static void main(String[] args){
numbers();
}
public static int[] numbers(){
int[] A = {1,2,3};
return A;
}
}
The code returns nothing at all. It’s driving me crazy!
It is returning the array, but all returning something (including an Array) does is just what it sounds like: returns the value. In your case, you are getting the value of
numbers(), which happens to be an array (it could be anything and you would still have this issue), and just letting it sit there.When a function returns anything, it is essentially replacing the line in which it is called (in your case:
numbers();) with the return value. So, what yourmainmethod is really executing is essentially the following:Which, of course, will appear to do nothing. If you wanted to do something with the return value, you could do something like this: