Learning Java – Step by Step

NB. IT SHOULD BE NOTED THAT I’M DOING THIS ON A LINUX MACHINE

JAVA

The general work flow is as follows

  1. Program is written by a java developer (file is a .java file)
  2. Program is compiled into bytecode by a compiler i.e. javac
  3. The compiled byte code is stored in a .class file by the compiler

JDK
Includes the JRE, compilers, JavaDoc, debuggers etc…To create, compile and run java programs we need this installed

JRE
Part of the JDK which lets us run a java program but won’t let you compile though (it includes a JVM)

 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.

  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism

Other features are

  1. Simplicity – uncomplicated features
  2. Robustness – emphasis on early error detection
  3. Secure – security flaws like stack corruption and buffer overflow are impossible to exploit
  4. Distributed – we can create distributed applications
  5. Multithreading support – allows concurrent execution of two or more parts of the program
  6. 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

  1. 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
  2. it should contain the content above …(save and quit)
  3. then use the java compiler to compile with the following command # javac HelloWorld.java
  4. if it runs successfully you should see a new file called HelloWorld.class (this is your bytecode
  5. 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

 

Leave a Reply

Your email address will not be published. Required fields are marked *