gcj

Implementation of programming language Java

gcj is the short nickname for GNU Java compiler, which is included in GNU Compiler Collection.

The Collection was extended to handle Java in 1998. It is distributed under GNU GPL and is an important example of free software. Development of gcj stopped in 2007 when Sun Java was released under GNU GPL.

gcj can compile Java source files to JVM bytecode or directly to machine codes as well as convert it to .class or .jar files.

To convert your Java code to a universal .class file, run gcj with -C option. The resulting .class file can be interpreted using gij interpreter.

GCC logo
GCC logo

Examples:

Hello, World!:

Example for versions Groovy 1.7, Sun Java 6, gcj 3.4.5
public class HelloWorld {
    public static void main(String[] args)
    {
        System.out.println("Hello, World!");
    }
}

Factorial:

Example for versions Groovy 1.7, Sun Java 6, gcj 3.4.5

This example uses recursive factorial definition.

public class Factorial {
    static long factorial(int n)
    {
        return ( n==0 ? 1 : n*factorial(n-1) );
    }
    public static void main(String[] args)
    {
        for (int n=0; n<=16; n++)
            System.out.println(n+"! = "+factorial(n));
    }
}

Factorial:

Example for versions Groovy 1.7, Sun Java 6, gcj 3.4.5

This example uses iterative calculation of factorial and illustrates the use of built-in class BigInteger which allows to handle arbitrarily large integer numbers.

import java.math.BigInteger;

public class Factorial {
    public static void main(String[] args)
    {
        BigInteger f = BigInteger.ONE;
        System.out.println("0! = " + f);
        for (int n=1; n<=16; n++)
        {   f = f.multiply(BigInteger.valueOf(n));
            System.out.println( n + "! = " + f);
        }
    }
}

Fibonacci numbers:

Example for versions Groovy 1.7, Sun Java 6, gcj 3.4.5

This example uses recursive definition of Fibonacci numbers.

public class Fibonacci {
    static int fibonacci(int n)
    {
        return (n<=2 ? 1 : fibonacci(n-1) + fibonacci(n-2));
    }
    public static void main(String[] args)
    {
        for (int n=1; n<=16; n++)
            System.out.print(fibonacci(n)+", ");
        System.out.println("...");
    }
}

Quadratic equation:

Example for versions Groovy 1.7, Sun Java 6, gcj 3.4.5

Java doesn’t provide complex numbers, so the calculations are done in real numbers with separate check for discriminant sign. This implementation allows to solve quadratic equations not only with integer coefficients but also with floating-point ones.

The coefficients are read from stream System.in, which allows only individual bytes to be read directly, so two wrapper classes InputStreamReader and BufferedReader are used to make reading more comfortable. The strings read from System.in are converted into double values using parseDouble method of class Double. In Java all input operations must be wrapped in try ... catch blocks to handle IOException — class of exceptions which are thrown by reading routines.

import java.util.*;
import java.io.*;

public class Quadratic {
    static String print(double real, double imag)
    {   if (Math.abs(imag)<1E-6)
            return ""+real;
        else 
            return "("+real+","+imag+")";
    }
    public static void main(String[] args)
    {   double A,B,C,D;
      try {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("A = ");
        A = Double.parseDouble(br.readLine());
        if (Math.abs(A)<1E-3)
        {   System.out.println("Not a quadratic equation.");
            return;
        }
        System.out.print("B = ");
        B = Double.parseDouble(br.readLine());
        System.out.print("C = ");
        C = Double.parseDouble(br.readLine());
      }
      catch (Exception e) {
        System.err.println("An error occured while reading input parameters.");
        return;
      }
        A = 2*A;
        D = B*B-2*A*C;
        if (Math.abs(D)<1E-3)
        {   System.out.println("x = "+(-B/A));
            return;
        }
        if (D>0)
            System.out.println("x1 = "+print((-B+Math.sqrt(D))/A, 0)+"\nx2 = "+print((-B-Math.sqrt(D))/A, 0));
        else
            System.out.println("x1 = "+print(-B/A,Math.sqrt(-D)/A)+"\nx2 = "+print(-B/A,-Math.sqrt(-D)/A));
    }
}