0% found this document useful (0 votes)
12 views

Lecture-01-JAVA

Java is a versatile, object-oriented programming language known for its portability, allowing code to run on any device with a Java Virtual Machine (JVM). Key features include platform independence, simplicity, robustness, security, and support for multithreading. The Java Development Kit (JDK) includes tools for development, while the Java Runtime Environment (JRE) provides the necessary libraries and JVM for running Java applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lecture-01-JAVA

Java is a versatile, object-oriented programming language known for its portability, allowing code to run on any device with a Java Virtual Machine (JVM). Key features include platform independence, simplicity, robustness, security, and support for multithreading. The Java Development Kit (JDK) includes tools for development, while the Java Runtime Environment (JRE) provides the necessary libraries and JVM for running Java applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Introduction to JAVA

Java is a widely used object-oriented programming language and software platform that runs on
billions of devices, including notebook computers, mobile devices, gaming consoles, medical
devices and many others. The rules and syntax of Java are based on the C and C++ languages.
One major advantage of developing software with Java is its portability. Once you wrote code for a
Java program on a notebook computer, it can be easily moved to a mobile device. When the
language was invented in 1991 by James Gosling of Sun Microsystems (later acquired by Oracle),
the primary goal was to be able to "write once, run anywhere."

Structure of Java Program


A basic Java program consists of several components that create a functional application.

public class FirstJavaProgram


{
public static void main(String args[])
{
System.out.println("Hello World");
}
}
Key Features of Java
1. Platform Independent

Compiler converts source code to byte code and then the JVM executes the bytecode
generated by the compiler. This byte code can run on any platform be it Windows, Linux, or
macOS which means if we compile a program on Windows, then we can run it on Linux and
vice versa. Each operating system has a different JVM, but the output produced by all the OS is
the same after the execution of the byte code. That is
why we call java a platform-independent language.

2. Object-Oriented Programming

Java is an object-oriented language, promoting the use of objects and classes. Organizing
the program in the terms of a collection of objects is a way of object-oriented programming, each
of which represents an instance of the class.
The four main concepts of Object-Oriented programming are:
•Abstraction

•Encapsulation

•Inheritance

•Polymorphism
3. Simplicity

Java’s syntax is simple and easy to learn, especially for those familiar with C or C++. It
eliminates complex features like pointers and multiple inheritances, making it easier
to write, debug, and maintain code.

4. Robustness

Java language is robust which means reliable. It is developed in such a way that it
puts a lot of effort into checking errors as early as possible, that is why the java compiler
is able to detect even those errors that are not easy to detect by another programming
language. The main features of java that make it robust are garbage collection,
exception handling, and memory allocation.

5. Security

In java, we don’t have pointers, so we cannot access out-of-bound arrays i.e it


shows ArrayIndexOutOfBound Exception if we try to do so. That’s why several
security flaws like stack corruption or buffer overflow are impossible to exploit in
Java. Also, java programs run in an environment that is independent of the os(operating
system) environment which makes java programs more secure.
6. Distributed
We can create distributed applications using the java programming language. Remote
Method Invocation and Enterprise Java Beans are used for creating distributed applications in java.
The java programs can be easily distributed on one or more systems that are connected to each
other through an internet connection.
7. Multithreading
Java supports multithreading, enabling the concurrent execution of multiple parts of a
program. This feature is particularly useful for applications that require high performance, such as
8. Portability
games and real-time simulations.
As we know, java code written on one machine can be run on another machine. The platform-
independent feature of java in which its platform-independent bytecode can be taken to any
platform for execution makes java portable. WORA(Write Once Run Anywhere) makes java
application to generates a ‘.class’ file that corresponds to our applications(program) but
contains code in binary format. It provides architecture-neutral ease, as bytecode is
independent of any machine architecture. It is the primary reason java is used in the
enterprising IT industry globally worldwide.

9. High Performance
Java architecture is defined in such a way that it reduces overhead during the runtime and at
some times java uses Just In Time (JIT) compiler where the compiler compiles code on-
demand basis where it only compiles those methods that are called making applications to
execute faster.
What is Byte Code in Java?
Byte Code in Java is an essential concept, serving as an intermediate representation
of Java code. This code isn't directly executed by the machine but is interpreted or
compiled into machine code by the Java Virtual Machine (JVM). The JVM interprets the
Byte Code, making Java platform independent.

This Byte Code is stored in .class files, which can be performed by the Java Virtual
Machine (JVM). The main purpose of Byte Code is to bridge the gap between the
human-readable source code and the machine-specific instructions that a computer's
processor understands.

Byte Code is not tied to any specific hardware or operating system (OS), making it a
critical factor in Java’s "write once, run anywhere" philosophy. The JVM is responsible
for interpreting or just-in-time compiling this Byte Code into native machine code
suitable for the specific platform on which the program is running.
How Does Byte Code Work?
To understand how Byte Code works, it's crucial to follow the process that occurs from the
moment you write a Java program to the point where it runs on your machine.

a) Compilation: When you write a Java program, the Java compiler (javac) converts the
human-readable source code into Byte Code, stored in ‘.class’ files. This Byte Code is
designed to be understood and executed by the JVM.

b) Execution: The JVM reads the Byte Code from ‘.class’ files and either interprets it or uses a
Just-in-time (JIT) compiler to convert it into native machine code. This optimises performance
by executing native code directly.

c) Platform Independence: Bytecode's power lies in its platform independence. The JVM
abstracts platform-specific details, allowing the same ‘.class’ files to run on any machine with
a JVM, enabling Java programs to run across different platforms without modification.
JDK, JRE and JVM
All three JDK, JRE and JVM are interdependent. JDK is Java Development Kit primarily meant for
Developers to develop Java based applications. JRE is Java Runtime Environment where Java
program runs. JDK carries JRE as an integral part of it. JRE can be installed seperately as well
on systems where no development is to be done and we only need to run the Java based
application or a java program is to be executed.

What is JDK?
JDK is an abbreviation for Java Development Kit which includes all the tools, executables,
and binaries required to compile, debug, and execute a Java Program. JDK is platform
dependent i.e. there are separate installers for Windows, Mac, and Unix systems. JDK includes
both JVM and JRE and is entirely responsible for code execution. It is the version of JDK that
represents a version of Java.

What is JRE?
JRE is a Java Runtime Environment which is the implementation of JVM i.e. the
specifications that are defined in JVM are implemented and create a corresponding
environment for the execution of code. JRE comprises mainly Java binaries and other classes
to execute the program like JVM which physically exists. Along with Java binaries JRE also
consists of various technologies of deployment, user interfaces to interact with code
executed, some base libraries for different functionalities, and language and
util-based libraries.
What is JVM?
 JVM (Java Virtual Machine) is an abstract machine that enables your computer to run a Java
program.
 When you run the Java program, Java compiler first compiles your Java code to bytecode.
Then, the JVM translates bytecode into native machine code (set of instructions that a
computer's CPU executes directly).
 Java is a platform-independent language. It's because when you write Java code, it's
ultimately written for JVM but not your physical machine (computer). Since JVM executes
JVMthe
is the
Java abbreviation for Java
bytecode which Virtual Machine which Java
is platform-independent, is aisspecification that provides a runtime
platform-independent.
environment in which Java byte code can be executed i.e. it is something that is abstract and its
implementation is independent of choosing the algorithm and has been provided by Sun and other
companies. It is JVM which is responsible for converting Byte code to machine-specific code. It can also run
those programs which are written in other languages and compiled to Java bytecode. The JVM performs the
mentioned tasks: Loads code, Verifies code, Executes code, and Provides runtime environment.
The JVM Architecture Explained
Every Java developer knows that bytecode will be executed by the JRE (Java Runtime Environment). But many
don't know the fact that JRE is the implementation of Java Virtual Machine (JVM), which analyzes the bytecode,
interprets the code, and executes it. It is very important, that we know the architecture of the JVM, as it enables us
to write code more efficiently.

What Is the JVM?


A Virtual Machine is a software implementation of a physical machine. Java was developed with the concept
of WORA (Write Once Run Anywhere), which runs on a VM. The compiler compiles the Java file into a
Java .class file, then that .class file is input into the JVM, which loads and executes the class file.

How Does the JVM Work?


JVM is divided into three main subsystems:
1.ClassLoader Subsystem
2.Runtime Data Area
3.Execution Engine
1. ClassLoader Subsystem
Java's dynamic class loading functionality is handled by the ClassLoader subsystem. It loads, links. and initializes the class file when it refers to a class for
the first time at runtime, not compile time.

1.1 Loading
Classes will be loaded by this component. BootStrap ClassLoader, Extension ClassLoader, and Application ClassLoader are the three ClassLoaders that will
help in achieving it.

1.BootStrap ClassLoader – Responsible for loading classes from the bootstrap classpath, nothing but rt.jar. Highest priority will be given to this loader.
2.Extension ClassLoader – Responsible for loading classes which are inside the ext folder (jre\lib).
3.Application ClassLoader –Responsible for loading Application Level Classpath, path mentioned Environment Variable, etc.

The above ClassLoaders will follow Delegation Hierarchy Algorithm while loading the class files.

1.2 Linking
4.Verify – Bytecode verifier will verify whether the generated bytecode is proper or not if verification fails we will get the verification error.
5.Prepare – For all static variables memory will be allocated and assigned with default values.
6.Resolve – All symbolic memory references are replaced with the original references from Method Area.

1.3 Initialization
This is the final phase of ClassLoading; here, all static variables will be assigned with the original values, and the static block will be executed.
2. Runtime Data Area
The Runtime Data Area is divided into five major components:
1.Method Area – All the class-level data will be stored here, including static variables. There is only one method area per JVM, and
it is a shared resource.
2.Heap Area – All the Objects and their corresponding instance variables and arrays will be stored here. There is also one Heap
Area per JVM. Since the Method and Heap areas share memory for multiple threads, the data stored is not thread-safe.
3.Stack Area– For every thread, a separate runtime stack will be created. For every method call, one entry will be made in the
stack memory which is called Stack Frame. All local variables will be created in the stack memory. The stack area is thread-safe
since it is not a shared resource. The Stack Frame is divided into three sub entities:
1. Local Variable Array – Related to the method how many local variables are involved and the corresponding values will
be stored here.
2. Operand stack – If any intermediate operation is required to perform, operand stack acts as runtime workspace to
perform the operation.
3. Frame data – All symbols corresponding to the method is stored here. In the case of any exception, the catch block
information will be maintained in the frame data.
4.PC Registers – Each thread will have separate PC Registers, to hold the address of current executing instruction once the
instruction is executed the PC register will be updated with the next instruction.
5.Native Method stacks – Native Method Stack holds native method information. For every thread, a separate native method
stack will be created.
3. Execution Engine
The bytecode, which is assigned to the Runtime Data Area, will be executed by the Execution Engine. The Execution
Engine reads the bytecode and executes it piece by piece.
1.Interpreter – The interpreter interprets the bytecode faster but executes slowly. The disadvantage of the
interpreter is that when one method is called multiple times, every time a new interpretation is required.

2.JIT Compiler– The JIT Compiler neutralizes the disadvantage of the interpreter. The Execution Engine will be using
the help of the interpreter in converting byte code, but when it finds repeated code it uses the JIT compiler, which
compiles the entire bytecode and changes it to native code. This native code will be used directly for repeated method
calls, which improve the performance of the system.
1.Intermediate Code Generator – Produces intermediate code
2.Code Optimizer – Responsible for optimizing the intermediate code generated above
3.Target Code Generator – Responsible for Generating Machine Code or Native Code
4.Profiler – A special component, responsible for finding hotspots, i.e. whether the method is called multiple
times or not.

3.Garbage Collector: Collects and removes unreferenced objects. Garbage Collection can be triggered by
calling System.gc(), but the execution is not guaranteed. Garbage collection of the JVM collects the objects that are
created.
Java Native Interface (JNI): JNI will be interacting with the Native Method Libraries and provides the Native
Libraries required for the Execution Engine.
Native Method Libraries: This is a collection of the Native Libraries, which is required for the Execution Engine.
What is JRE?
JRE (Java Runtime Environment) is a software package that provides
Java class libraries, Java Virtual Machine (JVM), and other components
that are required to run Java applications.
JRE is the superset of JVM.
What is JDK?
JDK (Java Development Kit) is a software development kit required to
develop applications in Java. When you download JDK, JRE is also
downloaded with it.
In addition to JRE, JDK also contains a number of development tools
(compilers, JavaDoc, Java Debugger, etc).
Relationship between JVM, JRE, and
JDK

You might also like