Let’s Understand Java

Fasrin Aleem
Nerd For Tech
Published in
3 min readOct 28, 2021

--

How Java Works?

For most programming languages, a program is translated (compiled) into a machine-language program. Then, the machine language program can be executed (or run).

Java works in the same way, compiling source code into bytecode first. The Java Virtual Machine can then compile the bytecode into machine code (JVM). The JVM allows Java’s bytecode to execute on any device, which is why it’s known as a “write once, run anywhere” language.

Of course, this is a slightly simplified version of how Java works. There’s actually much more to it.

Java program

In the diagram above, you can see that every operating system has a Java Virtual Machine on top of it, which is required to run a Java program. Because every operating system has its own Java Virtual Machine, it is a platform independent environment that simply transforms Java into code that a computer understands. If you want a more in-depth explanation of JVM architecture, check out my blog by clicking here.

Let’s understand this by the simple example below. The goal Is to write one application (an interactive party Invitation) and have It work on whatever device your friends have.

Java working process

Source: Make a document that will be used as a source. Make use of a well-established procedure (In this case, the Java language)

Compiler: Use a source code compiler to compile your document. The compiler checks for errors and won’t let you compile until It’s satisfied that everything will run correctly.

Output/Byte code: The compiler creates a new document, coded into Java bytecode. Any device that can run Java will be able to interpret and translate this file into something that it can use. The compiled bytecode is platform Independent.

JVM: Your friends don’t have a physical Java Machine, but they all have a virtual Java machine (implemented In software) running inside their electronic gadgets. The bytecode is read and executed by the virtual machine.

Code Structure in Java

Java code structure

Source file: A java source file can have several classes. Not more than one of these classes may be declared public. If the source file contains a public class, the source file’s name must be the public class’s name with a .java suffix. The java compiler creates a .class file for each class found in the source file.

Class file: A class has one or more methods.

Statements: Method has multiple statements.

Anatomy of a Class

Java anatomy
Short notes

References

[1] Head first java, 2nd Edition- eBook by Kathy Sierra & Bert Bates. Link -https://github.com/fasrinaleem/programming-ebooks/tree/master/Java

--

--