How java works
How java works
The working of the Java programming language can be summed up in three steps. Let’s go
through the steps given
below:
1. Here for the first step, we need to have a java source code otherwise we won't be able
to run the program you need to save it with the program.java extension.
2. Secondly, we need to use a compiler so that it compiles the source code which in turn
gives out the java bytecode and that needs to have a program.class extension. The
Java bytecode is a redesigned version of the java source codes, and this bytecode can
be run anywhere irrespective of the machine on which it has been built.
3. Later on, we put the java bytecode through the Java Virtual Machine which is an
interpreter that reads all the statements thoroughly step by step from the java bytecode
which will further convert it to the machine-level language so that the machine can
execute the code. We get the output only after the conversion is through.
So knowing about the working of java we need to understand the execution process of the
program written in the language.
2. Compiling a Java Program-Now once the program is created and does not have
any errors or mistakes we can go ahead and compile the program when you compile a
program it means the compiler compiles the program and if there are no errors after
compiling the program we can further run it and get the desired output. We compile
the Java program in a command prompt or another console.
3. Loading the Program into the Memory by Java Virtual Machine- A lot of memory is
required by JVM when you want to load the .class file extension before the execution.
Loading is the process of placing a program in memory for it to run. The .class files are
needed by the program to execute the file.
4. Java Virtual Machine verification for bytecode- Jvm has a bytecode verifier that is
due to maintenance of the security of the program. The bytecode verifies the code
only and only after the classes have been loaded in the memory to maintain the
security of the program. It makes sure that the bytecodes are valid and accessible. It
also saves the computer from various viruses and unsecure websites.
5. Java Program Execution-The above steps are executed by JVM when it interprets
the bytecode. Earlier JVM's were slow and only interpreted one bytecode at a time.
Nowadays the modern JVM's are much faster as they use JIT(just-in-time)
compilation units. These JVMs can execute various tasks at the same time. We also
call them HotSpot compilers as they are used by JVM to find out the hot spots in our
bytecode. Later on, the source code is converted bytecode into machine language.
Let’s understand the tools that come in handy when we code in the java programming
language.
1. Java Development Kit : As the name formally states Java development kit is a full-
time kit that has a compiler, Java Runtime Environment(JRE), Debuggers, Java
documents inclusive in it. For further execution in java, we need to have JDK
installed on our computers to further lead on to the creation, compilation, and running
of the java program. Here, as we use JDK we need an environment to run the
programs.We use JRE Java Runtime Environment which provides the least
requirements to execute the java program. It provides the JVM, Core classes and
supporting files.
Let’s have a look at the Java interpreter- As a compiler, an interpreter is also a programming
tool that converts our source code into readable(for computers) machine code but it doesn't
work in the same way that the compiler does. It converts each statement going through it
thoroughly before moving on to the next one and then executes it. No executor is needed for
an interpreter.
hence we can conclude that we can consider java to be both compiler and interpreter based
language because the source code first gets compiled into binary code and then the same
binary code runs on the JVm (Java Virtual Machine), and this is mostly a software-based
interpreter
So since we have completely understood how the codes in java are executed let's sum
up what we just read.