Factorial

Java Program

 public class Factorial {
	public static int fac(int n) {
		int result = 1;
		if(n > 1)
			result = n*fac(n-1);
		return result;
	}
	
	public static void main(String[] args) {
		System.out.println(fac(4));
	}
}

  

Java Disassembled Byte Code

Compiled from "Factorial.java"
public class Factorial {
  public Factorial();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."":()V
       4: return

  public static int fac(int);
    Code:
       0: iconst_1                          // push the constant 1 on the stack
       1: istore_1                          // pop the value off the stack and store it in variable 1 (result)
       2: iload_0                           // push the variable 0 (n) onto the stack
       3: iconst_1                          // push the constant 1 on the stack
       4: if_icmple     16                  // pop the top two values (n, 1) off of the stack and compare them
                                            // if n<1 branch to line 16
       7: iload_0                           // push n onto the stack
       8: iload_0                           // push n onto the stack
       9: iconst_1                          // push 1 onto the stack
      10: isub                              // pop two values and push their difference on the stack
      11: invokestatic  #16                 // Method fac:(I)I
      14: imul                              // pop two values, multiply them, push the result
      15: istore_1                          // pop the top and store as result
      16: iload_1                           // push the result on the stack (return value)
      17: ireturn

  public static void main(java.lang.String[]);
    Code:
       0: getstatic     #24                 // Field java/lang/System.out:Ljava/io/PrintStream;
       3: iconst_4                          // push the parameter on the stack
       4: invokestatic  #16                 // Method fac:(I)I
       7: invokevirtual #30                 // Method java/io/PrintStream.println:(I)V
      10: return
}