# Thread: Understanding Problem in Code !

1. Senior Member Join Date
Nov 2011
Posts
165
Rep Power
9

## Understanding Problem in Code !

Consider the following code:

Java 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:

Java 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:

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

Thanks  Reply With Quote

2. ## Re: Understanding Problem in Code ! Originally Posted by Jack_Tauson_Sr Why are they returning the following:

Java 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.  Reply With Quote

3. Senior Member Join Date
Nov 2011
Posts
165
Rep Power
9

## 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" ? 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.  Reply With Quote

4. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## 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  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•