JAVA and how Execution is done.

Introduction to JAVA

JAVA and how  Execution is done.

JAVA

JAVA is high-level, Object Oriented Programming language. Developed in the year 1995 by James Gosling and his team. It is widely used in various fields. It is used in mobile applications, web applications, desktop applications, games, web servers, Database connections etc.

Why Java

  • It is Simple.

  • It is Object-oriented. Everything is written in objects and their behaviors. Which makes it easy to code and solve and relate to real-world scenarios.

  • Portable its application can be run on many platforms.

  • Multi-threaded which allows building application that executes many tasks simultaneously.

  • Distributed- Java maintains TCP/IP protocol therefore used in distributed Internet environments.

  • Secure- Java allows applications built to be very secure and safe.

  • Due to its regular updates since 1995. Java has been improving and still comes as a top programming Language.

  • Due to its huge community and wide usage learning Java is easy and engaging.

  • Platform independent. Java is said to be WORA- Write Once Read Anywhere. The code is written once and can be executed on all platforms.

Execution of a JAVA code :

Before we began let's get familiar with these things- JDK-JAVA Development kit || JRE-JAVA Runtime Environment || JVM-Java virtual Machine || JIT-Just in time compiler ||Interpreter ||Garbage Collector.

Look at the below diagram

JDK

-when we first install Java there is an application/kit get installed in our system. Yes, it's JDK. Inside Jdk we have ie, JDK=JRE+Development tools. JDK provides an environment to develop and run Java Programs.

JRE

-it is an installation package that provides an environment to only run the program (JDK provides an environment to develop & JRE provides environment to Run). It consists of development technologies, integration libraries, and JVM.

JVM

JVM- which executes byte code(class file) to Machine level language by Interpreter(class loader). It has JIT, Interpreter and Garbage collector. JIT-it increases code execution when a method is been called multiple times. It increases efficiency by converting those repeated methods directly to Machine level language. Without the intervention of an interpreter.

Garbage collector- its job is simply to collect the garbage. It simply cleans the unreferenced objects meaning objects which are not in use.

Now let us understand more clearly. For that first, let's see Compiler vs Interpreter. Both give out Machine level language or Binary Language but differ in the way they do.

The interpreter is the one that converts to machine-level language line by line. Suppose you have written 100 lines of code the execution ie., conversion is done line by line 1st line is converted then 2nd line is converted and so on. ex:- Python.

The compiler is the one that converts the entire code to MLL. Suppose you have written 100 lines of code entire code at once is converted to MLL. Ex:- C.

In which category Java falls??..

Well, it uses both a Compiler and an Interpreter for the execution of the program.

Execution of Java file

  1. When we write code in Java. We need to have the file extension as .java. And it is necessary to have the file name the same as the class name.

  2. ex- if the main class name is "demo" the file name should be "demo.java" which is our source code

  3. To execute a file we first have a Java compiler(javac) we call the compiler which compiles the file. And generates a byte code or .class file.

  4. This byte code generated is platform-independent and can be shared and run on any platform.

  5. Now this generated byte code is further interpreted inside JVM(line by line) and executed.

  6. Note JVM is Platform-dependent.

Inside JVM, JVM reads the .class and verifies the file. Allocates memory for class variables, methods, objects etc. and executes the file.

Execution of source code to  MLL

Example:

E:\images>javac demo.java

Here javac is the compiler and the source file is a demo.java file. As soon as we call the compiler the .class file is created which is platform-independent.

To further execute the file we call the interpreter and execute bytecode.

E:\images>java demo

class demo {
 public static void main(String args[]) {
  System.out.println("Demo class file"); 
  }
}

Final Output of demo file.

Thank you for reading! Stay updated with my future content by subscribing. Your support means the world to me!

Did you find this article valuable?

Support Shrivatsa's Blog by becoming a sponsor. Any amount is appreciated!