Here is my problem. In the following code, I have a void
type method, but inside this method, I can still find a return
.
Also, in the recursion call, I can find this >>
operator.
So there are my questions :
- Why using a return in a void function ?
- Whats
number >> 1
does it mean ?
import java.util.Scanner;
class Code {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your Decimal value here : ");
int num = sc.nextInt();
printBinaryform(num);
System.out.println();
}
private static void printBinaryform(int number) {
int remainder;
if (number <= 1) {
System.out.print(number);
return; // KICK OUT OF THE RECURSION
}
remainder = number % 2;
printBinaryform(number >> 1);
System.out.print(remainder);
}
}