Thursday, April 26, 2018

Introduction to java: Brief History, characteristics, compilation and execution process

References/Links: http://pasted.co/af0d4289

Chapters

Java Brief History

Java was created by James Gosling and its team also known as green team in 1991 at Sun Microsystems."Java" was initially called "Oak" but was renamed in 1995 to "Java" which is now a part of Oracle Corporation. Java was designed to be platform-independent language that could be used to create software to be embedded in various electronic devices like remote controls and some appliances like a microwave oven.

In 1993 java design team used java on the internet that caused the focus of Java to switch from electronic devices to Internet programming which led to Java's success. Java was influenced by C++, If you're a C++ programmer you will be comfortable with java.

Java Characteristics as Programming Language

Java has its own structure, syntax, and paradigm. Java is a multi-paradigm programming language, Programming paradigm are a way to classify programming languages based on their features, Languages can be classified into multiple paradigms. Java's paradigm are: process-oriented, structured, imperative, generic, reflective, concurrent and object oriented programming(OOP) which is the core paradigm of java and it makes java coding much simplier.

Object Oriented Programming or OOP for short is a programming paradigm based on the concept of "objects" which may contain data in the form of fields, often known as attributes; and code, in the form of procedures often known as methods. You will learn more about OOP as you progress further. OOP has four principles: Abstraction, Encapsulation, Inheritance and Polymorphism; you will learn those principles as you progress further in learning java. I won't explain other paradigms because the focus of this tutorial is to give you some general knowledge about java and not to explain every paradigm here.

Java Features

 Here are some characteristics of java

Simple: java is easy to learn and the syntax is quite straightforward and java is easy to use.

Portable: java can run on browsers using applets, extends the capabilities of a server using java servlets, have a GUI for creating application like java Swing and can create an application on computers and mobile devices. Java is commonly used to create android app. Java is also a platform-independent, you can write a program once and run it anywhere.

Secured: here are some security features of java:

  • Java programs runs on virtual machine
  • Classloader adds security by separating the package for the classes locally and those that are imported from network.
  • Bytecode Verifier checks the bytecode fragments if there is an illegal code.
  • Security manager determines what resources a class can access.

Object-oriented: Java used object-oriented model paradigm.

Robust: Java has a strong memory management, garbage collection, exception handling and type checking that makes java robust.

Multithreaded: java supports multithreading - multithreading is a pretty advance topic in java

High performance: java bytecode makes java much faster than traditional interpretation but still a little bit slower than compiled language like C and C++.

Archtecture-Neutral: java can run on x86 and x64 architecture without worrying about implementation dependent features like fixed primitive types.

Statically Typed: Java variables has a specific type that is known at compile time unlike in dynamic typing where variables doesn't have a specific type, it means that every variables can be any type.

High-level language: Java syntax is closer to human readable language and easier to read and write than low-level languages.

Distributed: Remote Method Invocation(RMI) and Enterprise Java Bean(EJB) are used to create a distributed applications that is used to access files from any machine on the web.

Garbage Collection: garbage collection you can call it garbage collector,collector or GC for short is aform of automatic memory management that attempts to reclaim garbage, or memory that occupied by objects that are no longer in use by the program.

Java will handle the memory management for you so you can work on your project faster unlike some languages that has a manual memory management like c and c++.

Automatic and manual memory management have its advantages and disadvantages though.

How java converts codes to machine codes

Let's move on to the process of converting java codes to machine code, When we write a code in java, we can understand the code but our computer isn't, that's because computers can only understand binary numbers the "1" and "0" or "on" and "off" also called the machine code.

Programming language can implement a compiler or an interpreter or both when converting the language code to a machine code that computer understands. Compilers compile the whole code and convert it directly to a machine code while interpreters is a program that translate high level language code to a low level one line by line and executes code one at a time.

In depth explanation of compilers and interpreters are beyond to be covered in this tutorial, i only explained the general definition of those two. C and C++ implements a compiler while python,ruby and java implements an interpreter.

Java Compilation & Execution Process

java uses two compilation processes, Java compilation process starts at the compile-time environment, the source code that you wrote in the text editor with a file extension of .java will go through the java compiler or the javac.exe, we will see the javac and java.exe in action in future tutorials try to understand the process for now.

Java compiler will compile the source code into a bytecode, the bytecode also termed portable code or p-code is a highly optimized set of instructions designed for efficient execution by a software interpreter. Bytecodes can run on any platform as long as there is a Java Virtual Machine on that platform proving the java "Write once, run anywhere" principle is true.

When compiling is complete the bytecode will be contained in a file with a .class file extension which is called java class file. Now, we're done at the first compilation process of java the next compilation and execution process will be in the Java Virtual Machine.

Now that we have the bytecode of our source code we will now go to the run-time environment by executing java.exe, the next process is through the java classloader and to the bytecode verifier java classloader is a part of Java Runtime Environment the dynamically loads Java class files into the Java Virtual Machine.

Bytecode verifier will verify the bytecode first before loading it to the JVM. After passing through the java classloader and bytecode verifier, the bytecode will go through Java Virtual Machine, The Java Virtual Machine will execute the java bytecode, When the bytecode is loaded into the Java Virtual Machine some bytecode fragments will be interpreted and some will be compiled by Just-in-time compiler.

Just-in-time compiler or JIT continously analyses the code being executed and identifies parts of the code where the speedup gained from compilation would exceed the overhead of compiling that code. After that, the bytecode is now a machine code and will go to the machine's CPU to process and execute the code instructions.