0

I'm sure this is dumb, I'm just learning to code and trying to follow instructions on how to make this method that multiplies doubles into an array. The code compiles but does not return my array, it just gives me [D@76ed5528 which I'm assuming is the memory address of the array?

public class Ex1 {
    
    public static void main(String[] args) {
        Ex1 exone = new Ex1();
   
        double[] bob = exone.square(2, 6, 9, 8);
        System.out.println(bob);
    }
 
    public double[] square(int a, int b, int c, int d) { 
        double[] result = {a*a, b*b, c*c, d*d}; 
        return result; 
    }
}
vszholobov
  • 2,133
  • 1
  • 8
  • 23
  • Your problem (as you conceived it) is in *returning* the array, not "calling" it. If you use the wrong words you won't be able to find meaningful answers when you search. And you should always search before asking. But it turns out that the real problem is the way that you are *printing* the array. – Stephen C Aug 09 '21 at 04:35

2 Answers2

1

Yes @Drunkasaurus, the [D@76ed5528 you mentioned in is indeed the hash for the array in memory. In order to print the values in your array you have a few options including:

  1. Using the Arrays.toString() method: https://stackoverflow.com/a/29140403/10152919
  2. Using a simple loop: https://stackoverflow.com/a/409795/10152919

Also, no question is a dumb question if you can learn from it

avonbied
  • 17
  • 4
0

This is because you are directly trying to return the result array. Try to iterate over the array.

For ex - https://www.geeksforgeeks.org/iterating-arrays-java/