-2

This code is returning a memory address of an array being returned by the rgb2hsv function. I am not sure as to why this is, or if it is even a memory address that it is returning as I am familiar with memory addresses looking something along the lines of "0x038987086" but the code is returning something that looks more like this : "[F@12bb4df8", I am not sure as to why this is, if you could answer why and what it exactly what it is returning that would be extremely beneficial. Here is the code:

  public class HelloWorld{
     public static float max(float[] nums) {
        if (nums[0] > nums [1] && nums[0] > nums[2]) {
            return nums[0];
        }
        if (nums[1] > nums [0] && nums[1] > nums[2]) {
            return nums[1];
        }
        if (nums[2] > nums[0] && nums[2] > nums[1]) {
            return nums[2];
        }
        return 0;
        }
     public static float min(float[] nums) {
        if (nums[0] < nums [1] && nums[0] < nums[2]) {
        return nums[0];
        }
        if (nums[1] < nums [0] && nums[1]  < nums[2]) {
        return nums[1];
        }
        if (nums[2] < nums[0] && nums[2] < nums[1]) {
            return nums[2];
        }
        return 0; 
     }
     public static float[] rgb2hsv(float r,float g, float b) {
         float h;
         //Initializes h
         h=0;
         float s;
         float v;
         // Floats added to avoid operator precedence
         float x;
         float a;
         float hue;
         //Divides by 255 
         r=r/255;
         g=g/255;
         b=b/255;
         float[] rgbArray = {r,g,b};
         float mx= max(rgbArray);
         float mn= min(rgbArray);
         float df= mx-mn;
         if (mx == mn) {
            h=0; 
         }
         if (mx==r) {
            x=g-b;
            a=x/df+360;
            hue=a % 360;
            h=hue;
         }
         if (mx==g) {
        x=b-r;
        a=x/df+120;
        hue=a % 360;
        h=hue;
     }
     if (mx==b) {
        x=r-g;
        a=x/df+240;
        hue=a % 360;
        h=hue;
     }

     if(mx==0) {
         s=0;
     }
     else {
         s=df/mx;
     }
     v=mx;
     float[] hsvArray = {h,s,v};
     return hsvArray;
 }
 public static void main(String []args){
 float[] x=rgb2hsv(255, 255, 0);
 System.out.println(x);
 }

}

This should of outputted something along the lines of 3 distinct numbers telling me hue, value, and saturation. Instead it returns something similar to "[F@12bb4df8"

1 Answers1

-1

That's the toString of an array. See Object.toString, which will tell you that the default implementation returns the equivalent of

getClass().getName() + '@' + Integer.toHexString(hashCode())

Class.getName explains its part.

Object.hashCode will tell you more about the number. Typically it will be derived from the memory address of the object at the time of the first call. It probably wont be the actual memory address as the lower bits will typically be zero, and JVMs tend to move objects in memory as part of garbage collection.

The static method java.util.Arrays.toString or deepToString will give you something more sensible.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305