Wednesday, 12 January 2011

Calculate a PRIME NUMBER list using JAVA

This article shows how to write, compile, build and execute a program that calculates a list containing the first prime numbers using Java language.

We are going to create a source code file named
Then we copy/paste next programm in that file.

NOTE: Name is important, because in Java language file name has to match the class it contains.

import java.util.ArrayList;

 * Description of CalculatePrimeNumbers class.
public class CalculatePrimeNumbers {

    // By default it shows 50 first prime numbers.
    private static final int MAX_PRIMES = 50;

     * @param args the command line arguments
     * This function accepts zero or one arguments.
     * When called with no arguments it shows 50 first prime numbers.
     * If we specify a number, it shows that a prime number array with that specified length.
    public static void main(String[] args) {
        int top;

 // Parse command line arguments.
            case 0:
                top = MAX_PRIMES;
            case 1:
                top = Integer.parseInt(args[0]);
                System.err.println("ERROR: TOO MANY ARGUMENTS.");
        System.out.println("We are going to calculate " + top + " first prime numbers.");

 ArrayList<long> primes = new ArrayList<long>();
        primes.add(2L); // 2 is the first prime number we get.
 long index = 3; // Calculations start for number 3.

        System.out.println("Prime number list:");
        System.out.print(2 + " ");
        while (primes.size() < top) {
            boolean isPrime = true;

     for(long n:primes) {
                if ((index % n) == 0) {
                    isPrime = false;
            } // End for.

            if (isPrime) {
                // index stores a prime number.
                System.out.print(index + " ");

        } // End while.
    } // End main.

Java is a programming language that is compiled into bytecodes.

These bytecodes then are executed in a Java Virtual Machine.

Because of this architecture, Java bytecodes run in every system where the Java Virtual Machine has been ported.

There are three Java environments available in Debian:
  • sun-java6-jre, sun-java6-jdk
  • gcj-jre, gcj-jdk
  • openjdk-6-jre, openjdk-6-jdk

The Java Runtime Environment (JRE) provides the Java Virtual Machine.

Java compiler (javac) comes in the Java Development Environment (JDE).

sun-java6-jre sun-java6-jdk: official Sun Java environment.

openjdk-6-jre openjdk-6-jdk: Open Java Development Kit is a free and open source implementation of the Java programming language.

gcj-jre gcj-jdk: GNU compiler for Java (GNU Java Tools)

is a free software compiler for the Java programming language and a part of the GNU Compiler Collection (GCC).

gcj is able to generate from java source code either Java bytecodes or native machine code.

We install the default java environment which comes in debian sid distro for i386 architecture:

default-jdk: points to openjdk-6-jre for i386.

default-jre: points to openjdk-6-jdk for i386.

# aptitude install default-jdk

jdk provides javac tool, the Java compiler.

$ javac


$ javac -verbose

generates CalculatePrimeNumbers.class (some Java bytecodes)

We execute the Java bytecodes stored in the class file:

$ java CalculatePrimeNumbers

$ java CalculatePrimeNumbers 20


Javadoc is a tool that generates html documentation using the comments we write in the program.

Use javadoc in our CalculatePrimeNumbers example:

$ mkdir Doc

$ javadoc -verbose -d Doc


0 comentarios: