Introduction to JAVA
Dr. Anuja.R.Nair
Assistant Professor, CSE
1
Agenda
Introduction to JAVA
History
Platform independence
Java Virtual Machine
Java Architecture
Java Runtime Environment
Features of JAVA
Compiling and executing programs
2
Introduction to JAVA
High level programming language.
Operating system independent
Runs on Java Virtual Machine (JVM)
A secure operating environment that runs as a
layer on top of OS.
A sandbox which protects the OS from malicious
code.
Object oriented programming language
In Java, everything is a class.
Unlike C++, OOP support is a fundamental component
in Java.
3
History of JAVA
James Gosling, Mike Sheridan, and Patrick
Naughton and Sun Microsystems – Green
Team
Green Talk Oak Java
Java, May 20, 1995, Sun World
JDK Evolutions
J2SE, J2ME, and J2EE
4
Why JAVA was created?
The need for a platform independent
language that could be used to create
software to be embedded in various devices.
Different types of CPUs as controllers.
Designed for specific target, requires a full C++
compiler targeted for that CPU.
Time consuming and expensive.
World Wide Web
Internet version of C++.
5
JDK Editions
Java Standard Edition (J2SE)
J2SE can be used to develop client-side standalone
applications or applets.
Java Enterprise Edition (J2EE)
J2EE can be used to develop server-side
applications such as Java servlets and Java
ServerPages.
Java Micro Edition (J2ME)
J2ME can be used to develop applications for
mobile devices such as cell phones.
6
JDK Versions
There are many java versions that has been released. Current
stable release of Java is Java 21.
JDK Alpha and Beta (1995)
JDK 1.0 (23rd Jan, 1996)
JDK 1.1 (19th Feb, 1997)
J2SE 1.2 (8th Dec, 1998)
J2SE 1.3 (8th May, 2000)
J2SE 1.4 (6th Feb, 2002)
J2SE 5.0 (30th Sep, 2004)
Java SE 6 (11th Dec, 2006)
Java SE 7 (28th July, 2011)
Java SE 8 (18th March, 2014)
Java SE 9 (21st September, 2017)
Java SE 10 (20th March, 2018)
Java SE 11 (25th September, 2018)
Java SE 12 (19th March, 2019)
and many more …
7
Platform Independence
A platform is the hardware & software environment in which
a program runs
Once compiled, java code runs on any platform without
recompiling or any kind of modification
“Write Once Run Anywhere”
This is made possible by the Java Virtual Machine (JVM)
8
Java Virtual Machine
A Java virtual machine (JVM) is a process
virtual machine that can execute Java
bytecode. It is the code execution component
of the Java platform.
Java compiler converts the source code into
intermediate code which is called bytecode.
Machine specific code is generated by Java
interpreter by acting as an intermediary
between the virtual machine and real
machine.
9
Java Virtual Machine (JVM)
The source code of Java is stored in a text file with the
extension .java
The Java compiler compiles a .java file into byte code
The byte code will be in a file with extension .class
The generated .class file is the machine code of this
processor
Byte code is in binary language
The byte code is interpreted by the JVM
JVM makes Java platform independent
The JVM interprets the .class file to the machine language
of the underlying platform
The underlying platform processes the commands given
by the JVM
10
Java Virtual Machine
11
Java Architecture
JVM
Class Loader
Source File (HelloWorld.java)
Byte Code Verifier
JIT Code
Compiler (javac) Interpreter
Generator
Runtime
Byte code
(HelloWorld.class) Operating System
Hardware
12
Java Architecture
Class Loader : .class file to load into main memory from hard
disk.
Byte Code Verifier : is like a GateKeeper when downloading
classes from Internet check whether the code can harm your
machine or not.
JIT Code : frequently used compiled programs. Every time ,
first time it has to be given to interpreter only 2nd time
onwards JIT comes into picture that is already compiled
program if u need later on.
• Inside the browser also JVM is there.
13
The JVM Runtime Environment
The byte codes are stored in class files. At runtime, the
bytecodes that make up a java software program are loaded,
checked, and run in an interpreter. The interpreter has two
functions: it executes bytecodes, and makes the appropriate
calls to the underlying hardware.
The Java runtime environment runs code compiled for a JVM
and performs four main tasks:
Loads Code - Performed by the class loader
Verifies Code- Performed by the bytecode verifier
Executes Code - Performed by the runtime interpreter
Garbage Collection - De allocates memory not being used
14
Features of JAVA
Simple
Certain complex features like operator overloading, multiple
inheritance, pointers, explicit memory de allocation are not
present in Java language.
Secure
Absence of pointers
Architecture-neutral & portable
Java code compiled on Windows can be run on Unix without
recompilation
Object-oriented
Robust
Strictly/strongly typed language, Automatic garbage collection,
Exception handling
Multithreaded
Allows you to write programs that do many things simultaneously
15
Features of JAVA
Compiled & Interpreted
Java compiler translates Java code to Bytecode instructions
Java Interpreter generate machine code that can be directly
executed by machine that is running the Java program.
High performance
Translate directly into native machine code using JIT compiler
Distributed
Construct applications on networks which can contribute both
data and programs.
Java applications can open and access remote objects on
Internet easily. That means multiple programmers at multiple
remote locations to work together on single task.
Remote Method Invocation (RMI)
Dynamic
Dynamically linking in new class, libraries, methods and objects.
16
Comparison in JAVA and C++
17
JAVA Environment
Java environment includes a number of development tools,
classes and methods. The development tools are part of the
system known as Java Development Kit (JDK) and the classes
and methods are part of the Java Standard Library (JSL), also
known as the Application Programming Interface (API).
Java Development kit (JDK) – The JDK comes with a set of
tools that are used for developing and running Java program.
It includes:
1. Appletviewer( It is used for viewing the applet)
2. Javac(It is a Java Compiler)
3. Java(It is a java interpreter)
4.Javap(Java diassembler, which convert byte code into program
description)
5. Javadoc(It is for creating HTML document)
6. Jdb(It is Java debugger)
18
Difference between JDK, JRE and JVM
JAVA DEVELOPMENT KIT
The Java Development Kit (JDK) is a software
development environment used for developing Java
applications and applets. It includes the Java
Runtime Environment (JRE), an interpreter/loader
(Java), a compiler (javac), an archiver (jar), a
documentation generator (Javadoc) and other tools
needed in Java development.
JAVA RUNTIME ENVIRONMENT
JRE stands for “Java Runtime Environment”. The
Java Runtime Environment provides the minimum
requirements for executing a Java application; it
consists of the Java Virtual Machine (JVM), core
classes, and supporting files.
19
Difference between JDK, JRE and JVM
JAVA VIRTUAL MACHINE (JVM)
JVM(Java Virtual Machine) acts as a run-time
engine to run Java applications. JVM is the one that
actually calls the main method present in a java
code. JVM is a part of JRE(Java Runtime
Environment).
20
Difference between JDK, JRE and JVM
JDK – Java Development Kit (in short JDK) is Kit which
provides the environment to develop and execute(run) the
Java program. JDK is a kit(or package) which includes two things
Development Tools(to provide an environment to develop your
java programs)
JRE (to execute your java program).
JRE – Java Runtime Environment (to say JRE) is an
installation package which provides environment to only
run(not develop) the java program(or application)onto your
machine. JRE is only used by them who only wants to run the
Java Programs i.e. end users of your system.
JVM – Java Virtual machine(JVM) is a very important part of
both JDK and JRE because it is contained or inbuilt in both.
Whatever Java program you run using JRE or JDK goes into JVM
and JVM is responsible for executing the java program line
by line hence it is also known as interpreter.
21
Difference between JDK, JRE and JVM
22
Environment Variables in JVM
JAVA_HOME: Java Installation Directory
Used to derive all other environment variables
used by JVM
In Windows set JAVA_HOME=C:\jdk1.4.3
In UNIX export JAVA_HOME=/var/usr/java
CLASSPATH:
Used to locate class files
In Windows set CLASSPATH=%CLASSPATH%;%JAVA_HOME%\lib\tools.jar
In UNIX set CLASSPATH=$CLASSPATH:$JAVA_HOME/lib/tools.jar
23
Environment Variables in JVM
PATH
Used by OS to locate executable files
In Windows set PATH=%PATH%;%JAVA_HOME%\bin
In UNIX set PATH=$PATH:$JAVA_HOME/bin
24
Source File Layout - Hello World
Type the source code using any text editor
public class HelloWorldApp {
public static void main(String[]args){
System.out.println(“Hello World!”);
}
}
Save this file as HelloWorldApp.java
25
To Compile
Open the command prompt
Set the environment variables
Go to the directory in which the program is saved
Type - javac HelloWorldApp.java
If it says, “bad command or file name” then check the
path setting
If it returns to prompt without giving any message, it
means that compilation is successful
26
To Execute
Type the command - java HelloWorldApp
The result will be
27
Creating and Compiling Programs
Create/Modify Source Code
Source Code
Compile Source Code
i.e. javac Welcome.java
If compilation errors
Bytecode
Run Byteode
i.e. java Welcome
Result
If runtime errors or incorrect result
28
Compilation & Execution
Java
Program
(.java)
Java
Complier
(javac)
Byte Code
(.class file)
Interpreter Interpreter Interpreter
(java) (java) (java)
Win 32 Linux Mac
29
THANK YOU!!
ANY QUESTIONS??
30