Java

Intro

Java was developed by Sun Microsystems as a concurrent for the C-languages. It is platform-independent and object-oriented. That platform independence is their strength. It is both a compiled and interpreted language, because the bytecode from the compiler is interpreted by a JVM (Java Virtual Machine). It is a very small and easy-to-learn language, and from that it follows that there are a lot of libraries.

Sample program

The usual "Hello world" program…

public class Hello {
     public static void main(string[] args) {
          System.out.println("Hello world!");
     }
}

Compile this with javac.exe and then run with java.exe.

Applets

Applets are Java programs that can be included in HTML Web pages. When a Java Applet is run, it is transferred to your system, and run by the brower's JVM. Applets are embedded in HTML web pages by using the <applet> tag.

Interesting

  • You can easily write your own libraries
  • Your programs can run on any operating system with the Java Runtime Environment (JRE) installed.
  • Supports all OOP features, with the exception of manifold inheritance.

Library example (Complex Numbers by Mapar007).

This is a mini-library featuring a way to calculate sum and product from a complex number, and extract the imaginary and real part from it. You can download it below, in the "files" section. I also included UseComplex.class as an example client. (optimizing coming :p )

Compiling and Running

Compiling

  1. Save your program with the same name as the class it describes. (e.g. Complex.java)
  2. Set up the environment variable (Windows, needs only to be done once)
    1. Go to Start>Settings>Control Panel>System
    2. Go to the tab Advanced
    3. Click "Environment Variables"
    4. Edit "PATH": add the following path: <the path of your Java Development Kit>\bin
  3. Go to your command-line environment (for Windows: Start>Run>cmd)
  4. Navigate to the folder containing the program code (use the cd command on Windows)
  5. Type javac <name of your program>.java and press Enter
  6. If there are no error messages, your program should be compiled successfully, and should be residing in the same folder, saved as a file with the .class extension.

Running

  1. Compile your program (see above)
  2. Set up the environment variable PATH (Windows, needs only to be done once, for compiling and for running separately)
    1. Go to Start>Settings>Control Panel>System
    2. Go to the tab Advanced
    3. Click "Environment Variables"
    4. Edit "PATH": add the following path: <the path of your Java Runtime Environment>\bin
  3. Go to your command-line environment (for Windows: Start>Run>cmd)
  4. Navigate to the folder containing the .class file of the program (use the cd command on Windows)
  5. Type java <program name> WITHOUT .class behind it! Important!
  6. The program should be running.

Note: the arguments of the javac and java command are CASE SENSITIVE!!

Conventions

There are a few naming conventions that should be respected:

  • Constructors and class names start with a capital (this is PascalNaming)
  • Method and field names start with a lowercase character (this is camelNaming)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License