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

Unit1 Introduction JAVa

Uploaded by

Swastik Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Unit1 Introduction JAVa

Uploaded by

Swastik Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

ADVANCE JAVA PROGRAMMING

CIE-306T
-By Miss Sakshi Jha
Assistant Professor
Department of Computer Science and Engineering
SYLLABUS
OVERVIEW OF JAVA
• Java programming language was originally developed by Sun Microsystems which was initiated by James
Gosling and released in 1995 as core component of Sun Microsystems' Java platform (Java 1.0 [J2SE]).
• With the advancement of Java and its widespread popularity, multiple configurations were built to suit
various types of platforms. For example: J2EE for Enterprise Applications, J2ME for Mobile Applications.
• The new J2 versions were renamed as Java SE, Java EE, and Java ME respectively. Java is guaranteed to
be Write Once, Run Anywhere.
• Java is a technology consisting of both a programming language and a software platform. To create an
application using Java, you need to download the Java Development Kit (JDK), which is available for
Windows, macOS, and Linux. You write the program in the Java programming language, then a compiler
turns the program into Java bytecode—the instruction set for the Java Virtual Machine (JVM) that is a part of
the Java runtime environment (JRE). Java bytecode runs without modification on any system that supports
JVMs, allowing your Java code to be run anywhere.
• The Java software platform consists of the JVM, the Java API, and a complete development environment. The
JVM parses and runs (interprets) the Java bytecode. The Java API consists of an extensive set of libraries
including basic objects, networking and security functions; Extensible Markup Language (XML) generation;
and web services. Taken together, the Java language and the Java software platform create a powerful, proven
technology for enterprise software development.
CHARACTERISTICS OF JAVA

• Object Oriented − In Java, everything is an Object. Java can be easily extended since it is based on the Object model.
• Platform Independent − Unlike many other programming languages including C and C++, when Java is compiled, it is not
compiled into platform specific machine, rather into platform independent byte code. This byte code is distributed over the web
and interpreted by the Virtual Machine (JVM) on whichever platform it is being run on.
• Simple − Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it would be easy to master.
• Secure − With Java's secure feature it enables to develop virus-free, tamper-free systems. Authentication techniques are based on
public-key encryption.
• Architecture-neutral − Java compiler generates an architecture-neutral object file format, which makes the compiled code
executable on many processors, with the presence of Java runtime system.
• Portable − Being architecture-neutral and having no implementation dependent aspects of the specification makes Java portable.
Compiler in Java is written in ANSI C with a clean portability boundary, which is a POSIX subset.
• Robust − Java makes an effort to eliminate error prone situations by emphasizing mainly on compile time error checking and
runtime checking.
• Multithreaded − With Java's multithreaded feature it is possible to write programs that can perform many tasks simultaneously.
This design feature allows the developers to construct interactive applications that can run smoothly.
• Interpreted − Java byte code is translated on the fly to native machine instructions and is not stored anywhere. The development
process is more rapid and analytical since the linking is an incremental and light-weight process.
• High Performance − With the use of Just-In-Time compilers, Java enables high performance.
• Distributed − Java is designed for the distributed environment of the internet.
• Dynamic − Java is considered to be more dynamic than C or C++ since it is designed to adapt to an evolving environment. Java
programs can carry extensive amount of run-time information that can be used to verify and resolve accesses to objects on run-
time.
C++ V/S JAVA
Comparison Index C++ Java
Platform-independent C++ is platform-dependent. Java is platform-independent.
Mainly used for C++ is mainly used for system programming. Java is mainly used for application programming. It is widely used in
Windows-based, web-based, enterprise, and mobile applications.
Design Goal C++ was designed for systems and applications programming. It was Java was designed and created as an interpreter for printing systems
an extension of the C programming language. but later extended as a support network computing. It was designed to
be easy to use and accessible to a broader audience.
Goto C++ supports the goto statement. Java doesn't support the goto statement.
Multiple inheritance C++ supports multiple inheritance. Java doesn't support multiple inheritance through class. It can be
achieved by using interfaces in java.
Operator Overloading C++ supports operator overloading. Java doesn't support operator overloading.
Pointers C++ supports pointers. You can write a pointer program in C++. Java supports pointer internally. However, you can't write the pointer
program in java. It means java has restricted pointer support in java.
Compiler and Interpreter C++ uses compiler only. C++ is compiled and run using the compiler Java uses both compiler and interpreter. Java source code is converted
which converts source code into machine code so, C++ is platform into bytecode at compilation time. The interpreter executes this
dependent. bytecode at runtime and produces output. Java is interpreted that is
why it is platform-independent.
Call by Value and Call by C++ supports both call by value and call by reference. Java supports call by value only. There is no call by reference in java.
reference
Structure and Union C++ supports structures and unions. Java doesn't support structures and unions.
Thread Support C++ doesn't have built-in support for threads. It relies on third-party Java has built-in thread support.
libraries for thread support.
Documentation comment C++ doesn't support documentation comments. Java supports documentation comment (/** ... */) to create
documentation for java source code.
Virtual Keyword C++ supports virtual keyword so that we can decide whether or not to Java has no virtual keyword. We can override all non-static methods by
override a function. default. In other words, non-static methods are virtual by default.
unsigned right shift >>> C++ doesn't support >>> operator. Java supports unsigned right shift >>> operator that fills zero at the
top for the negative numbers. For positive numbers, it works same like
>> operator.
Inheritance Tree C++ always creates a new inheritance tree. Java always uses a single inheritance tree because all classes are the
child of the Object class in Java. The Object class is the root of the
inheritance tree in java.
Hardware C++ is nearer to hardware. Java is not so interactive with hardware.
Object-oriented C++ is an object-oriented language. However, in the C language, a Java is also an object-oriented language. However, everything (except
single root hierarchy is not possible. fundamental types) is an object in Java. It is a single root hierarchy as
everything gets derived from java.lang.Object.
In Java, any line starting with // is a
comment. Comments are intended for
JAVA PROGRAM users reading the code to understand the
intent and functionality of the program.
It is completely ignored by the Java
compiler (an application that translates
// Your First Program Java program to Java bytecode that
class HelloWorld computer can execute).

{ In Java, every application begins with a class definition. In


the program, HelloWorld is the name of the class.Remember
public static void main(String[] args) that every Java application has a class definition, and the
name of the class should match the filename in Java.
{
System.out.println("Hello, World!"); 1. public
It is an Access modifier, which specifies from where and who can access the method. Making the main() method
} public makes it globally available. It is made public so that JVM can invoke it from outside the class as it is not
present in the current class.
2. static
} It is a keyword that is when associated with a method, making it a class-related method. The main() method is static
so that JVM can invoke it without instantiating the class. This also saves the unnecessary wastage of memory which
would have been used by the object declared only for calling the main() method by the JVM.
3. void
The code above is a print statement. It prints the text Hello, World! to It is a keyword and is used to specify that a method doesn’t return anything. As the main() method doesn’t return
anything, its return type is void. As soon as the main() method terminates, the java program terminates too. Hence,
standard output (your screen). The text inside the quotation marks is
it doesn’t make any sense to return from the main() method as JVM can’t do anything with the return value of it.
called String in Java.
4. main
Notice the print statement is inside the main function, which is inside It is the name of the Java main method. It is the identifier that the JVM looks for as the starting point of the java
the class definition. program. It’s not a keyword.
5. String[] args
It stores Java command-line arguments and is an array of type java.lang.String class. Here, the name of the String
array is args but it is not fixed and the user can use any name in place of it.
JAVA JDK,JRE AND JVM

JVM (JAVA VIRTUAL MACHINE)


• JVM (Java Virtual Machine) is an abstract machine that enables your computer to run a Java
program.
• It is called a virtual machine because it doesn't physically exist. It is a specification that
provides a runtime environment in which Java bytecode can be executed. It can also run those
programs which are written in other languages and compiled to Java bytecode.
• JVMs are available for many hardware and software platforms. JVM, JRE, and JDK are
platform dependent because the configuration of each OS is different from each other.
However, Java is platform independent.
• 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 the Java
bytecode which is platform-independent, Java is platform-independent.
There are three notions of the JVM: specification, implementation, and instance.
1. A specification where working of Java Virtual Machine is specified. But implementation
provider is independent to choose the algorithm. Its implementation has been provided
by Oracle and other companies.

2. An implementation Its implementation is known as JRE (Java Runtime Environment). Fig. Relationship between JDK,JVM & JRE
3. Runtime Instance Whenever you write java command on the command prompt to run
the java class, an instance of JVM is created.

The JVM performs the following main tasks:

4. Loads code

5. Verifies code

6. Executes code

7. Provides runtime environment


JVM ARCHITECTURE
1) CLASSLOADER
Classloader is a subsystem of JVM which is used to load class files. Whenever we run the java program, it is loaded first by the
classloader. There are three built-in classloaders in Java.
1.Bootstrap ClassLoader: This is the first classloader which is the super class of Extension classloader. It loads the rt.jar file
which contains all class files of Java Standard Edition like java.lang package classes, java.net package classes, java.util package
classes, java.io package classes, java.sql package classes etc.
2.Extension ClassLoader: This is the child classloader of Bootstrap and parent classloader of System classloader. It loades the jar
files located inside $JAVA_HOME/jre/lib/ext directory.
3.System/Application ClassLoader: This is the child classloader of Extension classloader. It loads the class files from class path.
By default, classpath is set to current directory. You can change the classpath using "-cp" or "-classpath" switch. It is also known
as Application classloader.
2) CLASS(METHOD) AREA
Class(Method) Area stores per-class structures such as the runtime constant pool, field and method data, the code for methods.
3) Heap
It is the runtime data area in which objects are allocated.
4) STACK
Java Stack stores frames. It holds local variables and partial results, and plays a part in method invocation and return. Fig. JVM Architecture
Each thread has a private JVM stack, created at the same time as thread.
A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes.
5) PROGRAM COUNTER REGISTER
PC (program counter) register contains the address of the Java virtual machine instruction currently being executed.
6) NATIVE METHOD STACK
It contains all the native methods used in the application.
7) EXECUTION ENGINE
It contains:
4.A virtual processor
5.Interpreter: Read bytecode stream then execute the instructions.
6.Just-In-Time(JIT) compiler: It is used to improve the performance. JIT compiles parts of the byte code that have similar
functionality at the same time, and hence reduces the amount of time needed for compilation. Here, the term "compiler" refers to a
translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.
8) JAVA NATIVE INTERFACE
Java Native Interface (JNI) is a framework which provides an interface to communicate with another application written in
another language like C, C++, Assembly etc. Java uses JNI framework to send output to the Console or interact with OS libraries.
JVM AS AN INTERPRETER AND EMULATOR

• Java processor can be implemented as software! It is implemented as a program that reads the bytecodes and performs the operations they
specify. This is another type of interpreter. Some interpreters run source code written in a high level language like Basic; others (like the Java
interpreter) run bytecodes.
• This second type of interpreter is sometimes called an emulator because it emulates hardware, but in fact is software. A Java bytecode
interpreter can be created for any computer system. Once you have a Java compiler and a Java interpreter you can run any Java program no
matter what type of computer you have.
• JVM stands for Java Virtual Machine and is the part of a Java system which emulates a platform independent system. It hides all aspects of the
underlying system from the Java code. This allows Java code to run on completely different platforms such as an AS/400 or a Sun workstation
as easily as it would on a PC or Macintosh. Normal software would have to have specific versions built for each different machine, especially
when different CPU’s are involved. In essence the JVM replaces the CPU specifics and OS tasks with a generic Java CPU and Java OS that
works the same on any machine.
• The designers of Java chose to use a combination of compiling and interpreting. Programs written in Java are compiled into machine language,
but it is a machine language for a computer that doesn't really exist. This so-called "virtual" computer is known as the Java Virtual Machine, or
JVM. The machine language for the Java Virtual Machine is called Java bytecode. There is no reason why Java bytecode couldn't be used as
the machine language of a real computer, rather than a virtual computer. But in fact the use of a virtual machine makes possible one of the
main selling points of Java: the fact that it can actually be used on any computer. All that the computer needs is an interpreter for Java
bytecode. Such an interpreter simulates the JVM in the same way that a C64 emulator simulates a Commodore 64 computer. (The term JVM is
also used for the Java bytecode interpreter program that does the simulation, so we say that a computer needs a JVM in order to run Java
programs. Technically, it would be more correct to say that the interpreter implements the JVM than to say that it is a JVM.)
JRE (Java Runtime Environment)

• JRE is an acronym for Java Runtime Environment. It is also


written as Java RTE. The Java Runtime Environment is a set
of software tools which are used for developing Java
applications. It is used to provide the runtime environment.
It is the implementation of JVM. It physically exists. It
contains a set of libraries + other files that JVM uses at
runtime.
• 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. Fig. Java Runtime Environment
• JRE is the superset of JVM.
• If you need to run Java programs, but not develop them, JRE
is what you need.
JDK (JAVA DEVELOPMENT KIT)
• JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is a software development environment which is used to
develop Java applications and applets. It physically exists. It contains JRE + development tools.
• JDK is an implementation of any one of the below given Java Platforms released by Oracle Corporation:
 Standard Edition Java Platform
 Enterprise Edition Java Platform
 Micro Edition Java Platform
• The JDK contains a private Java Virtual Machine (JVM) and a few other resources such as an interpreter/loader (java), a compiler (javac), an
archiver (jar), a documentation generator (Javadoc), etc. to complete the development of a Java Application.
COMPILATION AND EXECUTION OF JAVA PROGRAM

•Step 1: Writing the code in NetBeans IDE. As you can see in


the diagram step 1 is to actually type the code. In this image
above you can see we have our code file as abc.java
•Step 2: Once you have written the code you save it and click
Run (if you are using Netbeans IDE otherwise you compile the
code in the command prompt by using the command javac
abc.java). This invokes the Java Compiler. The compiler
checks the code for syntax errors and any other compile time
errors and if no error is found the compiler converts the java
code into an intermediate code(abc.class file) known
as bytecode. This intermediate code is platform
independent (you can take this bytecode from a machine
running windows and use it in any other machine running
Linux or MacOS etc). Also this bytecode is an intermediate
code, hence it is only understandable by the JVM and not the
user or even the hardware /OS layer.
•Step 3: This is the start of the Run Time phase, where the
bytecode is loaded into the JVM by the class loader(another
inbuilt program inside the JVM).
•Step 4: Now the bytecode verifier(an inbuilt program inside
the JVM) checks the bytecode for its integrity and if no issues
are found passes it to the interpreter.
•Step 5: Since java is both compiled and interpreted language,
now the interpreter inside the JVM converts each line of the
bytecode into executable machine code and passed it to the
OS/Hardware i.e. the CPU to execute.
WORKING OF JIT (JUST-IN-TIME) COMPILER

•Case 1:
• In case 1 you can see that we are at the intepretation phase (Step 5 of the overall
program execution). Lets assume we have 5 lines which are supposed to be
interpreted to their corresponding machine code lines. So as you can see in the Case
1 there is no JIT involved. thus the interpreter converts each line into its
corresponding machine code line. However if you notice the last 2 lines are the same
(consider it a redundant line inserted by mistake). Clearly that line is redundant and
does not have any effect on the actual output but yet since the interpreter works line
by line it still creates 5 lines of machine code for 5 lines of the bytecode.
• Now this is inefficient right? lets see case 2
•Case 2:
• In case 2 we have the JIT compiler. Now before the bytecode is passed onto the
interpreter for conversion to machine code, the JIT compiler scans the full code to
see if it can be optimized. As it finds the last line is redundant it removes it from the
bytecode and passes only 4 lines to the interpreter thus making it
more efficient and faster as the interpreter now has 1 line less to interpret.
• So this is how JIT compiler speeds up the overall execution process.

This was just one scenario where JIT compiler can help in making the execution process fast and
efficient. There are other cases like inclusion of only those packages needed in the code, code
optimizations, redundant code removal etc which overall makes the process very fast and
efficient. Also different JITs developed by different companies work differently and JIT
compilers are an optional step and not invoked every time.
So this was the complete Execution Process of Java Program in Detail with the Working of JIT
Compiler.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy