Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
0 views

Module_1_1

java module one intro

Uploaded by

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

Module_1_1

java module one intro

Uploaded by

All about Gaming
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

BCSE103E-COMPUTER

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

• Programming is a process of planning and creating a program


• Computer Program-A sequence of statements whose objective is to accomplish a
task
• Programming language-A set of rules, symbols and special words used to
construct programs.
• Low level languages-Assembly language and machine language
• Middle level languages-C, C++
• High level languages-C#,Fortran,Python,Java etc.

4
GENERAL TERMINOLOGIES

• Assembly language: An instruction is an easy to remember form called a


mnemonic(Eg.ADD,LOAD)
• Machine Language :Elemental language of computers
• Generally-instructions are written in assembly language
• Assembler: Translates assembly language instructions into machine language
• Compiler: A program that translates a program written in high level language into the
equivalent machine language
• In JAVA , the compiler translates instruction into bytecode(machine language of JAVA).
• Bytecode-intermediate language –instructions are first translated into bytecode and then
interpreted into a particular machine language.
5
Self Awareness Quiz
• The operating system is the first program laoded into the computer when the
power is turned on—T/F???
• Assembly language is the language that uses mnemonics for its instructions-
T/F??
• A program written in a high level language is called a source program-T/F??
• The first step in problem solving process is to code the problem—T/F??

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

• Special type of Java program that moves


around Internet and gets download and run in
any Internet browser
• Server-initiated program
• Dynamic object transmits from server to client
side
• Display the server provided data
• Read user inputs
9
Java Application Program
• Sum of two numbers
• Checking even or odd
• Multiplication of two numbers etc.,

10
Java Application Program
public class AddTwoNumbers
{

public static void main(String[ ]args)


{

int num1 = 5, num2 = 15, sum;


sum = num1 + num2;

System.out.println("Sum of these numbers: "+sum);


}
}

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

Object- JAVA Features • Run on different client


Architectural-
Oriented Neutral
computers without harm
them
Dynamic Interpreted • Java ensures the access
limit within Java
High
Distributed
Performance Execution Environment
14
Simple Simple
Secure Robust

Multi-
• Easy to learn and use
Portable
Threaded

Object- JAVA Features Architectural- • Moving from C++ to


Oriented Neutral Java is easier since Java
inherits many Object
Dynamic Interpreted
Oriented features from
C++
High
Distributed
Performance
15
Robust
Simple • Error checking at
Secure Robust compile and run time
Multi- • Memory management
Portable
Threaded problem – automatic
deallocation and garbage
Object- JAVA Features Architectural- collection for unused
Oriented Neutral
objects

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
{

public static void main(String[ ]args) Heading of the method main


{
System.out.println(―My first Java Program.‖);
}
}
23
• OUTPUT
My first Java program.
How to save a Java Program ?
ASimpleJavaProgram.java

24
More Examples
public class Addition
{

public static void main(String[ ]args)


{
System.out.println(―The sum of 2 and 3=‖+5);
}
}
25
• Guess the output???

26
• Output
The sum of 2 and 3 =5

27
public class ASimpleProgram
{

public static void main(String[ ]args)


{
System.out.println(―7 + 8 = ‖ + (7 + 8));
}
}
28
• OUTPUT
7 + 8 = 15

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

• Output of Java Compiler is Byte Code rather than Executable code


• Highly optimized set of instructions run by Java run-time System
(JVM – Java Virtual Machine)
• JVM – Interpreter for bytecode
• JVM differs at different platforms
• This makes a Java as Portable among Web
33
Bytecode

• Security ensured by JVM – Controls program execution


• Provides restricted program execution environment – Sandbox  from where
program can‘t perform other unauthorized actions to the machine
• Java Program  Byte Code (Intermediate Representation) by the Compiler  Run
by JVM from anywhere
• Just-In-Time Compiler (RIT) of Hotspot: At run time, a selected portion of the
bytecode into executable code by the compiler

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

• Performs verification, preparation, and (optionally) resolution.


• Verification: It ensures the correctness of the .class file i.e. it checks whether this file is
properly formatted and generated by a valid compiler or not. If verification fails, we get run-
time exception java.lang.VerifyError. This activity is done by the component
ByteCodeVerifier. Once this activity is completed then the class file is ready for compilation.
• Preparation: JVM allocates memory for class static variables and initializing the memory to
default values.
• Resolution: It is the process of replacing symbolic references from the type with direct
references. It is done by searching into the method area to locate the referenced entity.

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.

JVM (Java Virtual Machine) is a very


important part of both JDK and JRE because it
JDK (Java Development Kit) is a Kit that provides is contained or inbuilt in both. Whatever Java
the environment to develop and execute(run) the program you run using JRE or JDK goes into
Java program. JDK is a kit(or package) that includes JVM and JVM is responsible for executing the
two things java program line by line, hence it is also known
 Development Tools(to provide an environment as an interpreter.
to develop your java programs)
 JRE (to execute your java program).

46
Program Paradigms
Program Paradigms

Process – oriented Model Object-oriented

(Code acting on Data) (Program around its Data)


Ex: C Ex: Java, C++

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.

• Object-Oriented Programming is the backbone of Java.


• Mastering OOP concepts like inheritance, encapsulation, and polymorphism is critical for writing scalable Java code

• Pillars of OOPS in Java


• 1. Abstraction

• 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

You might also like