Key Ideas
[Part 1]
Although there is a lot in this introductory part, the key ideas are fairly simple. They are:
- A computer program is a set of instructions for the computer telling it what to do. In most high-level languages the instructions are written in something humans can easily (well, reasonably easily!) understand in one or more text files. The file/s are then converted into binary data that a computer can understand.
- In some cases the text is converted to binary once and run (compilation), but in others it is converted each time the program runs (interpretation). Binary information has to be specific to a type of computer, so, in Java, you compile the code to something not-quite binary called "bytecode", which you can send to anyone. This is then interpreted into binary by the Java Runtime Environment interpreter.
- The fundamental unit of code in an Object Orientated Language is the class. Each class does some job, and each is contained (usually) in its own text file. The basic layout of a class looks like this:
public class ClassName {
}
This would be in a file called ClassName.java.
- One class must act as the ignition system for the whole program and contain a "main" block. While you compile all classes, you only pass one, this "main" class, into the interpreter to run the code. As we'll see in the practical, the way to do this is:
javac Class1.java
javac Class2.java
java Class1
- The main class and block look like this:
public class ClassName {
public static void main (String args[]) {
}
} - Within a class, the code is divided up by blocks (in bold black below):
public class HelloWorld {
public static void main (String args[]) {
System.out.println("Hello World");
}
} - Blocks usually start with a declaration (in bold black below):
public class HelloWorld {
public static void main (String args[]) {
System.out.println("Hello World");
}
} - Elements not either blocks or declarations are usually statements ending in a semicolon (in bold black below):
public class HelloWorld {
public static void main (String args[]) {
System.out.println("Hello World");
}
}