How does Java work?

How does Java work?

Java architecture simple explanation

What the hell is Java 🍵 ?

Java is a popular high-level programming language used to develop all kinds of software, like web, mobile, desktop applications, and games. It's designed to be portable, meaning it can run on any platform that has a Java Virtual Machine (JVM). This is possible because Java code is compiled into bytecode that can be run on the JVM. Java is also known for its security, reliability, and performance, and has a large and active community. Overall, Java is a powerful and versatile language and platform independent, its’ a good choice for many types of projects.

Why do we need programming language?

Basically computers can only understand machine code i.e., 0’s and 1’s. For computers 1’s mean “true” or “on” and “false” or “off” for 0’s. Machine code is also known as binary code which is hard to understand for us human beings in order to communicate with the computer. That’s where programming languages come to rescue. Most programming languages are either high-level(close to human languages(e.g. Arabic, English, French etc.) or low-level(close to binary(0’s and 1’s) but not fully).Programming languages make it easier for us to communicate and give instructions to computer to perform any sort of computational or user desired task in a language that is similar to the user’s language (e.g. English).

Pre-requisites

  • Java Development kit (JDK) should be installed on your pc to run java. Here’s a great tutorial by @freecodecamp.
  • A text editor such as vs code or an IDE(Integrated development environment) IntelliJ Idea(recommended).(although you could even write the code in notepad , it’s a really tiring process. So for a smooth dev experience install an IDE)

Now that we’re done installing JDK on our pc, let’s dive into what’s inside this whole java thingy.

How does Java work?

a) Source Code: The Java source code is the main Java code written in plain text (following syntax rules) in an IDE or text editor. The source code contains classes and methods which define the behaviour and structure of the program. The file is saved with .java file name extension

b)Compilation:

  • The .java file is compiled using the Java compiler(comes with JDK) into a language understood by the computer called bytecode
  • Compiler error checks and generates bytecode instructions for the JVM (java virtual machine). JVM is responsible for the java’s platform independence.

c)Java Virtual machine (JVM):

  • It is a crucial component of Java’s Architecture which is responsible for executing bytecode on different platforms.
  • Bytecode lies between high-level & low-level languages. Consider it as Java’s own language for program execution.
  • It is available for various platforms.
  • It makes java the “Write once, run anywhere” language.

meme

d)Bytecode Execution:

  • JVM loads bytecode line by line when program runs.
  • Bytecode is converted to machine code(0’s and 1’s :x)by the JIT (Just in time) compiler in JVM.
  • JIT optimizes code during runtime for better performance

e)Memory Management:

  • Java comes with automatic memory management via a process called Garbage collection .
  • Basically an object without a reference variable will be removed. (Kinda like how an empty packet of chips is thrown in the 🗑️).
  • It saves developers from the hassle of manual memory management and saves time.
  • JVM auto tracks and de-allocates memory that is no longer in use. (More about memory management in an upcoming blog).

jdk

Credit - @kunalstwt

f) Java Class Library :

  • Java comes with JCL(Java Class Library or Java API(Application Programming Interface). -It provides a bunch of pre-built classes & methods used to perform various tasks such as Input/0uptu operations, networking, file handling, graphics & more. Developers make use of these Libraries to speed up development process and also to avoid rewriting common functionalities(simple programs).

e) Platform Independence:

Since Java Bytecode is executed by JVM, it allows a Java program not the .java but the .class file of the Java program(which is platform and OS independent) to run on any platform(with compatible JVM version installed).

References:

  • Chat GPT: “Explain Java Architecture in a simple and easy manner in a chronological order with relevant examples?”
  • Article by geeksforgeeks on java’s program execution and compilation
  • Kunal Kushwaha’s YouTube video on Java Architecture.

Conclusion:

Overall, Java's architecture combines the use of a high-level programming language, a bytecode format, and a virtual machine to provide a portable and efficient environment for developing and running applications.