34

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!

6
  • 30
    The numbers method returns the int array, but you're doing nothing with the returned array. What do you expect, a magical force that will print the array in the console? Oct 13, 2012 at 3:44
  • I'm running it in my main method I thought this would return the array
    – Atreyu
    Oct 13, 2012 at 3:45
  • 2
    You aren't doing anything with the array when you get it...but it is returning the array as you expect.
    – Makoto
    Oct 13, 2012 at 3:49
  • 6
    just add int[] array = numbers(); in main and print this array variable
    – Antarix
    Oct 13, 2012 at 4:01
  • He's probably familiar with Python or something, where if you were to type, "myVariable" it would print the contents of the variable automagically, nomatter what type it was.
    – ADJenks
    Mar 28, 2016 at 20:06

6 Answers 6

41

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 your main method is really executing is essentially the following:

public static void main(String[] args) {
    {1,2,3};
}

Which, of course, will appear to do nothing. If you wanted to do something with the return value, you could do something like this:

public static void main(String[] args){
    int[] result = numbers();
    for (int i=0; i<result.length; i++) {
        System.out.print(result[i]+" ");
    }
}
9
  • I need to keep the method as one that returns a double[], is it possible not to print out the actual entries individually?
    – Atreyu
    Oct 13, 2012 at 3:54
  • Absolutely; printing out the individual entries was just an example of what one might do with the return value. The main point is that when you call a function that returns something, you need to actually do something with that return value (for example: print it, store it for later, pass it to another function...)
    – AlexFZ
    Oct 13, 2012 at 3:55
  • Alright, I guess I get it, it's just weird that returning an int, or String for example, would actually output the int, or string
    – Atreyu
    Oct 13, 2012 at 3:57
  • 1
    That's just Java for you. In some languages (mainly interactive ones), you can just type in a function or the name of a variable and have the value printed to standard out. In Java (and many languages like it), nothing is implicitly printed or used externally (as @Stephen C said); you need to do something with every value yourself.
    – AlexFZ
    Oct 13, 2012 at 4:01
  • 3
    "Alright, I guess I get it, it's just weird that returning an int, or String for example, would actually output the int, or string" - Actually, it WOULD NOT output them. At least it wouldn't do that unless you expressly told it to.
    – Stephen C
    Oct 13, 2012 at 4:11
13

Of course the method numbers() returns an array, it's just that you're doing nothing with it. Try this in main():

int[] array = numbers();                    // obtain the array
System.out.println(Arrays.toString(array)); // now print it

That will show the array in the console.

3
  • In case anyone was wondering, System.out.println(array.toString()); doesn't work. it returns the address.
    – Dimenein
    Aug 9, 2020 at 12:47
  • 1
    @Dimenein Nope, that won’t print the array in a useful way. Try it and you’ll see what I mean ;) Aug 9, 2020 at 12:49
  • Yeah I just edited it, my bad
    – Dimenein
    Aug 9, 2020 at 12:50
8

You have a couple of basic misconceptions about Java:

I want it to return the array without having to explicitly tell the console to print.

1) Java does not work that way. Nothing ever gets printed implicitly. (Java does not support an interactive interpreter with a "repl" loop ... like Python, Ruby, etc.)

2) The "main" doesn't "return" anything. The method signature is:

  public static void main(String[] args)

and the void means "no value is returned". (And, sorry, no you can't replace the void with something else. If you do then the java command won't recognize the "main" method.)

3) If (hypothetically) you did want your "main" method to return something, and you altered the declaration to allow that, then you still would need to use a return statement to tell it what value to return. Unlike some language, Java does not treat the value of the last statement of a method as the return value for the method. You have to use a return statement ...

7

As Luiggi mentioned you need to change your main to:

import java.util.Arrays;

public class trial1{

    public static void main(String[] args){
        int[] A = numbers();
        System.out.println(Arrays.toString(A)); //Might require import of util.Arrays
    }

    public static int[] numbers(){
        int[] A = {1,2,3};
        return A;
    }
}
4
  • Actually this still does nothing... I want it to return the array without having to explicitly tell the console to print
    – Atreyu
    Oct 13, 2012 at 3:50
  • Main calls numbers() method and numbers creates an array, then it returns that array back to main and assigns it to the A. There is no printing?
    – Kairan
    Oct 13, 2012 at 3:54
  • No you're right it's just that how come there is no output at all?
    – Atreyu
    Oct 13, 2012 at 3:55
  • Ahh, sorry - you meant to print. Using Luiggi method to call the Arrays.toString() method will take in an array and convert it to a String object, which you can then use the System.out.println(); method to print it out. Updated my code post.
    – Kairan
    Oct 13, 2012 at 4:08
1

If you want to use the numbers method, you need an int array to store the returned value.

public static void main(String[] args){
    int[] someNumbers = numbers();
    //do whatever you want with them...
    System.out.println(Arrays.toString(someNumbers));
}
1

You need to do something with the return value...

import java.util.Arrays;

public class trial1{

    public static void main(String[] args){
        int[] B = numbers();
        System.out.println(Arrays.toString(B));
    }

    public static int[] numbers(){
        int[] A = {1,2,3};
        return A;
    }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.