# Understanding Problem in Code !

• 03-25-2013, 02:53 AM
Jack_Tauson_Sr
Understanding Problem in Code !
Consider the following code:

Code:

```public class Aufg3 {     public static void main(String[] args) {         int[] test = decToBin(12, getBinArray(12));         for(int i = 0; i < test.length; i++){             System.out.println(test[i]);         }     }     public static int[] getBinArray(int number){         int res = number, length = 0;         while(res != 0){                    res /= 2;                     length++;         }         return new int[length];     }     public static int[] decToBin(int number, int[] array){         int res = number, k = array.length-1;         while(res != 0){             if(res%2 == 0){                 array[k] = 0;             }else{                 array[k] = 1;             }             k--;             res /= 2;         }         return array;     } }```

IF we consider the following part of code:

Code:

``` public static int[] getBinArray(int number){         int res = number, length = 0;         while(res != 0){                    res /= 2;                     length++;         }         return new int[length];     }```

Why are they returning the following:

Code:

`return new int[length];`
Please explain as I haven't understood it.

Thanks
• 03-25-2013, 02:54 AM
Fubarable
Re: Understanding Problem in Code !
Quote:

Originally Posted by Jack_Tauson_Sr
Why are they returning the following:

Code:

`return new int[length];`

Who are "they" and how are they "returning the following"?

If you mean "the compiler" and if you're seeing an error, then by all means you should post the error for us to see and review.
• 03-25-2013, 03:08 AM
Jack_Tauson_Sr
Re: Understanding Problem in Code !
"They" means the person who has written the code. I'm trying to understand it. Shouldn't it be returning just "length" ?

Quote:

Originally Posted by Fubarable
Who are "they" and how are they "returning the following"?

If you mean "the compiler" and if you're seeing an error, then by all means you should post the error for us to see and review.

• 03-25-2013, 03:20 AM
jim829
Re: Understanding Problem in Code !
Take a look at the method return value. It is int[]. This means that it is returning an array of ints. If it was just returning the length, the return value would just be an int.

Regards,
Jim