NB. IT SHOULD BE NOTED THAT I’M DOING THIS ON A LINUX MACHINE
The general work flow is as follows
- Program is written by a java developer (file is a .java file)
- Program is compiled into bytecode by a compiler i.e. javac
- The compiled byte code is stored in a .class file by the compiler
Includes the JRE, compilers, JavaDoc, debuggers etc…To create, compile and run java programs we need this installed
Part of the JDK which lets us run a java program but won’t let you compile though (it includes a JVM)
This is what executes the byte code generated by the compiler – THE PROGRAM RUN PHASE
The compiler (javac) converts source code (.java file) to byte code (.class file). As mentioned above.
Each O.S has its own type of JVM but the all produce a bytecode which is same across all O.S’s which is why Java is called “Platform independent”
Java as an Object Oriented Language.
Object oriented programming is a way of organizing programs as a collection of objects, each of which represents an instance of a class.
There are 4 main concepts of Object Oriented programming and they are.
Other features are
- Simplicity – uncomplicated features
- Robustness – emphasis on early error detection
- Secure – security flaws like stack corruption and buffer overflow are impossible to exploit
- Distributed – we can create distributed applications
- Multithreading support – allows concurrent execution of two or more parts of the program
- Portability – can be run on various machines
Java is a high level programming language that cannot be run directly on any machine as it needs to first be translated into that particular machines’ language.
Javac does this translation (from .java files to bytecode i.e. .class file)
The JVM is a vm that sits inside our machine and the machine language for the JVM is the byte code THUS its primary function is to execute the byte code produced by the compiler
Below is the java work flow
Below is a simple java program.
Output: This is my first program in java
How to achieve this
- on your linux server (in a directory of your choice), create a file ending in .java… – this will be your .java file (in my case i saved the file as HelloWorld.java
- it should contain the content above …(save and quit)
- then use the java compiler to compile with the following command # javac HelloWorld.java
- if it runs successfully you should see a new file called HelloWorld.class (this is your bytecode
- Finally, you run this command # java HelloWorld and you should get the output “This is my first program in java”
Note that i ran the command (in number 5) without the .java OR .class extension
So let’s break down our written code
- public class FirstJavaProgram # This states the class which in this case is called FirstJavaProgram….. note that you can call this anything
- public static void main # This is the standard main method which fires up the program code
- System.out.println(“xxxxx”); # This is the println method, javas way of writing to the console