Table of contents
The running mechanism:
- You create a java document with .java extension.
- You run the document through a javac compiler which compiles the code into a new class file (.class extension), checks for errors and if there is no error, it will run.
- A new document is generated by this compiler which is coded into bytecode. This compiled bytecode is platform-independent.
- Then, anyone with a Java Virtual Machine (JVM) can run it.
The Source code:
public class Employees {
public static void main (String[] args) {
// your code here
}
}
Every Java application needs to have at least one class, and at least one
main method.
You instruct your JVM to load the Employees class, then execute the main() method.
You’ll type your source code file (with a .java extension), then compile it into a new class file (with a .class extension). When you run your program, you’re really running a class.
- (Save) Employees.java ---> (Compile) javac Employees.java ---> (Run) java Employees
Basic syntax
- Statements end in a semicolon ;
- Code blocks are defined by a pair of curly braces { }
Data types:
- int myNum = 5; // Integer (whole number (4 bytes)
- long myLongNum = 1234454657; // long can store very big numbers (8 bytes).
- float myFloatNum = 5.99f; // note the f at the end - can hold up to 7 decimal digits (32 bits)
- double myDoubleNum = 5.99; // can hold up to 15 decimal digits (64 bits)
- char myLetter = 'D'; // Character (single quotes)
- boolean myBool = true; // Boolean
- String myText = "Hello"; // String (double quotes)
- The assignment operator is one equals sign =
- The equals operator uses two equals signs ==
- A while loop:
while (x == 4) {
// as long as x is 4, keep doing something
}