01 ~ Introduction To JAVA

01 ~ Introduction To JAVA

In previous blog We talked about What language computer knows, What is Programming and why do we actually need it. and I told you that we will start with JAVA and Golang Simultaneously. So here it is we are starting with JAVA first and then we will move forward with Golang also.

What is JAVA ?

Java is a programming language used to instruct computers to perform various tasks. It can be used for many purposes, including:

  • Building mobile applications

  • Web development

  • Building cloud applications

  • Creating binaries for blockchains (Hyperledger Besu, for example, is built using Java)

  • Developing applications for AI and IoT devices

And there are many more things you can do with Java that I can't even list them all!

How java code executes?

We write our Java program in a file with the .java extension. This .java file is human-readable and is referred to as the source code.

When we tell the Java program (Java binary) to execute the file, we are essentially instructing the Java software on our computer to carry out the tasks written in the source code. The Java software comes with both a compiler and an interpreter. The compiler takes the entire code and converts it into a .class file. This .class file contains the bytecode, which is the compiled version of the source code.

Now, this bytecode is translated into machine code (binary language, i.e., 0’s and 1’s) line by line. The task of converting bytecode into machine code is handled by the Java interpreter.

Why JAVA converts source code in byte code ?

The question arises: why doesn't Java convert the source code directly into machine code? Why go through the extra step of converting it into bytecode first?

The answer lies in one of Java's key features: platform independence. This means you can run Java code on any type of machine. For example, you can write Java code on a Windows operating system and then run the same code on a Mac. This is made possible by the Java Virtual Machine (JVM). We'll explore the JVM in more detail later in this blog.

Architecture of JAVA

JDK

The Java JDK is a package that can be downloaded from the internet. It helps in running Java code and provides the environment to develop and execute Java programs.

This package includes the following:

  • Development tools to create your program

  • JRE (Java Runtime Environment) to execute your program

  • A compiler known as javac

  • An archiver called jar

  • A documentation generator called javadoc

  • An interpreter and loader

JRE

It is an installation package that provides the environment to run Java programs.

It consists of:

  • Development technologies

  • User interface toolkit

  • Integration libraries

  • Base libraries

  • JVM (Java Virtual Machine)

JVM

The Java Virtual Machine (JVM) is a virtual machine that allows computers to run Java programs compiled to Java bytecode. It provides an abstract computing machine that enables Java programs to be executed on any device or operating system without requiring modification.

Think of JVM as whole another level of operating system which is only designed to run JAVA byte code (.class file)

JIT

JIT (Just-In-Time) compilation is a performance optimization technique used by the Java Virtual Machine (JVM) and other runtime environments. It improves the execution speed of programs by converting bytecode (the intermediate code generated after compiling source code) into native machine code at runtime, just before it's needed. This way, the program runs faster compared to interpreting the bytecode directly.

Steps that happens when you run a java program

When you execute a Java program, a series of steps happen behind the scenes to turn the code you write into something the computer can understand and run. Let’s break it down simply:

1. You Write Java Code

  • You write a program in a file with a .java extension. This code is written in human-readable form, which the computer can’t understand directly.

2. Compilation: Turning Code into Bytecode

  • You run a Java compiler (like javac), which takes your .java file and converts it into something called bytecode. This bytecode is stored in a .class file.

  • Bytecode is not specific to any machine. It’s an intermediate code that the JVM can read.

  • Think of bytecode as a universal language that can run on any device that has a Java Virtual Machine (JVM) installed.

3. JVM Starts: The Java Virtual Machine

  • When you want to run your Java program, the JVM kicks in. The JVM’s job is to take the bytecode and run it on the machine you are using (like your laptop, phone, or server).

  • The JVM is like an interpreter that translates this universal bytecode into something your machine can execute, regardless of whether you’re on Windows, Mac, Linux, etc.

4. Interpreting or Compiling (JIT)

  • At first, the JVM might interpret the bytecode, reading and executing it one line at a time. This makes the program run, but it can be a bit slow.

  • To make things faster, the JVM uses JIT (Just-In-Time) compilation. It notices which parts of your code are used often (called "hot spots") and compiles those parts into native machine code, which is specific to your computer’s processor. Native machine code is what your hardware understands and can run super-fast.

  • So, the JVM dynamically switches between interpreting less critical parts of your program and compiling frequently used code to boost speed.

5. Execution: Talking to the Hardware

  • Once the bytecode is translated (either interpreted or compiled into machine code), it finally runs on your computer’s processor (CPU).

  • The machine code interacts with your computer's memory, input/output devices, and other hardware to actually perform the actions described in your program, like showing text on a screen or calculating numbers.

6. Memory Management and Garbage Collection

  • While your program runs, the JVM takes care of memory. It allocates memory for the things your program creates (like variables and objects) and automatically cleans up memory when it’s no longer needed. This process is called Garbage Collection, and it helps prevent memory leaks.
Summary :-
  • You write Java code.

  • The code is compiled into bytecode.

  • The JVM reads the bytecode and either interprets it line by line or compiles frequently used parts into machine code using JIT.

  • The compiled machine code runs directly on your hardware.

  • The JVM manages memory and other resources while your program is running.

This whole process allows Java programs to be portable (run anywhere) and efficient, while also handling complex tasks like memory management behind the scenes!

Now that’s it for this blog in next one we will start with writing with our first JAVA program and our journey with java begins.

Until then, keep learning, keep sharing, and keep supporting!