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
- Save your program with the same name as the class it describes. (e.g. Complex.java)
- Set up the environment variable (Windows, needs only to be done once)
- Go to Start>Settings>Control Panel>System
- Go to the tab Advanced
- Click "Environment Variables"
- Edit "PATH": add the following path: <the path of your Java Development Kit>\bin
- Go to your command-line environment (for Windows: Start>Run>cmd)
- Navigate to the folder containing the program code (use the cd command on Windows)
- Type javac <name of your program>.java and press Enter
- 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
- Compile your program (see above)
- Set up the environment variable PATH (Windows, needs only to be done once, for compiling and for running separately)
- Go to Start>Settings>Control Panel>System
- Go to the tab Advanced
- Click "Environment Variables"
- Edit "PATH": add the following path: <the path of your Java Runtime Environment>\bin
- Go to your command-line environment (for Windows: Start>Run>cmd)
- Navigate to the folder containing the .class file of the program (use the cd command on Windows)
- Type java <program name> WITHOUT .class behind it! Important!
- 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)