Module_1_1
Module_1_1
PROGRAMMING:JAVA
Dr. ARIVARASI A, SENSE, VIT, Chennai, India
MODULE-1-JAVA BASICS
• Features of JAVA Language
• JVM
• Bytecode
• OOP Paradigm
• JAVA Programming structure
• JAVA Programming Constructs
• Data types
• Variables & Operators
• JAVA naming Conventions
2
Module Outcome
• Explain the various concepts involved in writing a Java Program
• Develop simple Java Programs
3
COMPUTER PROGRAMMING-A RECAP
4
GENERAL TERMINOLOGIES
6
Problem Soving Process
7
History of JAVA
• Patrick Naughton et al developed ―OAK‖ in 1991 at Sun
Microsystems, Inc.
• ―Oak‖ renamed as ―JAVA‖ in 1995
• Java = familiar syntax of ‗C‘ + Object oriented features of
‗C++‘
• Internet version of C++
• Java-two types of programs-Applications and Applets
8
Java Applets
10
Java Application Program
public class AddTwoNumbers
{
11
Simple
JAVA Features
Robust
Secure • Platform – Independent
Multi-
programming language
Portable
Threaded • Portable program for
WWW (World Wide
Object- JAVA Features Architectural- Web)
Oriented Neutral
• Internet Programming
(Architecture – Neutral
Dynamic Interpreted programming language)
High
Distributed
Performance
12
Simple
Portability
Secure Robust
• Portable executable code
Portable
Multi- among different CPUs
Threaded and Operating Systems
Object- JAVA Features Architectural-
Oriented Neutral • Same code works on
different devices and
Dynamic Interpreted platforms
High
Distributed
Performance
13
Security
Simple
Robust
Secure • Portability makes
Multi-
computer to download
Portable malicious programs
Threaded
Multi-
• Easy to learn and use
Portable
Threaded
Dynamic Interpreted
• Exceptional Conditions –
Object oriented
Distributed
High Exception handling
Performance
mechanism 16
Simple
Object-Oriented
Secure Robust
• Object – oriented
Portable
Multi- programming
Threaded
language rather than
Object- JAVA Features Architectural- data-oriented
Oriented Neutral
• Object model is
Dynamic Interpreted
simple and easy to
extend
High
Distributed
Performance
17
Multithreaded
Simple
Secure Robust
• For interactive and
Multi- networked programs
Portable
Threaded
• Perform simultaneous
Object- JAVA Features Architectural- tasks
Oriented Neutral
• Java run-time system
provides solution for
Dynamic Interpreted
multiprocess
Distributed
High synchronization
Performance
18
Architecture -
Secure
Simple
Robust Neutral
Multi- • OS or processor upgrade
Portable
Threaded makes a program as mal-
function
Object- JAVA Features Architectural-
Oriented Neutral
• JVM creates a Java
program as ―write – once;
Dynamic Interpreted
run – anywhere, anytime
High and forever‖
Distributed
Performance
19
High Performance
Simple
Secure Robust
• Cross-platform programs
Portable
Multi- are compiled as Byte code;
Threaded an intermediate
representation
Object- JAVA Features Architectural-
Oriented Neutral • Byte code runs on JVM
• Byte code easily converted
Dynamic Interpreted into machine code for very
high performance by just-in-
High time (JIT) compiler
Distributed
Performance
20
Distributed
Simple
Secure Robust • Remote Method
Invocation (RMI)
Multi-
Portable
Threaded enables to access
methods over the
Object- JAVA Features Architectural- network
Oriented Neutral
• Accessing
resources
Dynamic Interpreted
using URL in the
Internet
High
Distributed
Performance
21
Simple
Dynamic
Secure Robust
• Have run-time type
Portable
Multi- information i.e.,
Threaded access an object at run
Object- JAVA Features time
Architectural-
Oriented Neutral • Small portion of
dynamic Byte code
Dynamic Interpreted that is updated at run
High time
Distributed
Performance
22
A simple Java program
public class ASimpleJavaProgram Name of the Java Class
{
24
More Examples
public class Addition
{
26
• Output
The sum of 2 and 3 =5
27
public class ASimpleProgram
{
29
Processing a JAVA Program
30
• Editor-Create a Java Program-SOURCE PROGRAM
• Compiler –Checks the source program for errors, correctness of syntax.
• If error is found it indicates it..else translates the program into bytecode
• The bytecode is saved in the file with the .class extension
• Loader-Connects the bytecode for the classes used in the program
• Also loads Java program‘s bytecode into main memory
• Interpreter-Translates each bytecode instruction into machine language and
executes it.
31
JAVA Virtual Machine - JVM
Source
Byte Code JVM
Program Compiler
Editor (Running/
(Compilation)
Execution)
Executable
Library File
32
Bytecode
34
JVM
• JVM(Java Virtual Machine) runs Java applications as a run-time engine. JVM is
the one that calls the main method present in a Java code. JVM is a part of
JRE(Java Runtime Environment).
• Java applications are called WORA (Write Once Run Anywhere). This means a
programmer can develop Java code on one system and expect it to run on any other
Java-enabled system without any adjustment. This is all possible because of JVM.
• When we compile a .java file, .class files(contains byte-code) with the same class
names present in .java file are generated by the Java compiler. This .class file goes
into various steps when we run it. These steps together describe the whole JVM.
35
36
Class Loader Subsystem
• It is mainly responsible for three activities.
• Loading
• Linking
• Initialization
37
Loading
• The Class loader reads the ―.class” file, generate the corresponding binary
data and save it in the method area. For each ―.class” file, JVM stores the
following information in the method area.
• The fully qualified name of the loaded class and its immediate parent class.
• Whether the ―.class” file is related to Class or Interface or Enum.
• Modifier, Variables and Method information etc.
38
Linking
39
Initialization
• In this phase, all static variables are assigned with their values defined in the
code and static block(if any). This is executed from top to bottom in a class
and from parent to child in the class hierarchy.
40
Dr.A.Menaka Pushpa, VIT, Chennai, India 41
JVM Memory Areas
42
Execution Engine
• Execution engine executes the ―.class” (bytecode). It reads the byte-code line by line, uses
data and information present in various memory area and executes instructions. It can be
classified into three parts:
• Interpreter: It interprets the bytecode line by line and then executes. The disadvantage here is that when
one method is called multiple times, every time interpretation is required.
• Just-In-Time Compiler(JIT) : It is used to increase the efficiency of an interpreter. It compiles the entire
bytecode and changes it to native code so whenever the interpreter sees repeated method calls, JIT
provides direct native code for that part so re-interpretation is not required, thus efficiency is improved.
• Garbage Collector: It destroys un-referenced objects.
43
Java Native Interface (JNI)
• It is an interface that interacts with the Native Method Libraries and provides
the native libraries(C, C++) required for the execution. It enables JVM to call
C/C++ libraries and to be called by C/C++ libraries which may be specific
to hardware.
44
JDK
• The Java Development Kit (JDK) is a cross - platformed software
development environment that offers a collection of tools and libraries
necessary for developing Java-based software applications and applets. It is a
core package used in Java, along with the JVM and the JRE (Java Runtime
Environment).
45
JRE (Java Runtime Environment) is an
installation package that provides an
environment to only run(not develop) the
java program(or application)onto your machine.
JRE is only used by those who only want to run
Java programs that are end-users of your
system.
46
Program Paradigms
Program Paradigms
47
OOPS Paradigm
• Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism,
etc. in programming.
• The main aim of OOPs is to bind together the data and the functions that operate on them so that no other
part of the code can access this data except that function.
• 2. Encapsulation
• 3. Inheritance
• 4. Polymorphism
48
OOP Element - Abstraction
• Handle Complexity by Abstraction
• Hierarchical Abstraction/Classification of
Complex System
• Ex: Car – Single Object
• Data into Component Objects (own behaviours)
• Process divided into messages among objects
• Messages instruct object to do a action
49
1. OOP Principles - Encapsulation
• Safe mechanism to bind together the code and its data from outside misuse
• Class : Collection of Objects/Defines the structure and behaviour of the data
and code
Class
• Object: Instance of the class
Public: Non-member
Data Methods from
Method outside of the
class
Private:
Members of the Class Data
Method 50
2. OOP Principles - Inheritance
Grand Father Grand Mother
• Object inherits property (Height) (Skin Colour)
from one or more other Super
objects Class
• Superclass derived Father Mother
Subclasses ( Knowledge) (H/SC+ Music)
• Subclass inherited
properties from super
classes along with its own Son
Specialization (K + Music + Sub
Sports) Class
51
3. OOP Principles - Polymorphism
• Polymorphism – many forms
• One Interface, multiple methods
• Create generic interface to group of related activities
• General Class Action into Specific action by the
compiler
• Ex: Stack for different data types
52