Chapter 1 Advanced Programming Part I
Chapter 1 Advanced Programming Part I
Introduction to Java
(Review)
Mulugeta G.
Introduction
Java
Object-Oriented Architecture-Neutral
Distributed Portable
Robust Multithreaded
3
Types of Java Applications
4 type of applications can be created using java
1. Standalone Application
• It is also known as desktop application or window-based
application.
• An application that we need to install on every machine such as
media player, antivirus etc.
• AWT and Swing are used in java for creating standalone
applications.
4
Types of Java Applications
2. Web Application
• runs on the server side and used to create dynamic pages
• Currently, servlet, jsp, struts, jsf etc. technologies are used for
creating web applications in java.
5
Types of Java Applications
3. Enterprise Application
• An application that is distributed in nature, [ banking apps] .
• Has advantage of high security, load balancing and clustering.
• EJB, JSP, Servlet and JPA are used for creating enterprise apps.
4. Mobile Application
• An application that is created for mobile devices.
• Currently Android and Java ME [Micro Edition] are used for
creating mobile applications.
6
Java Terminology
Java Development Kit
• It contains one (or more) JRE's along with the various
development tools like
o compilers,
o deployment tools,
o debuggers, development libraries, etc.
o Is a super set of JRE
7
Java Terminology
Java Runtime Environment
• It is used to provide runtime environment.
• A software on your computer that actually runs Java programs.
• JRE = It contains set of libraries + other files that JVM uses at
runtime.
8
Java Terminology
Java Virtual Machine
• Its an abstract machine.
• It interprets the bytecode into machine code
• Main Tasks of JVM are:
• Loads code
• Executes code
• Verifies code
• Provides runtime environment
9
Java Interpreter
• Java is a little different.
• Java compiler produces bytecode not machine code.
• Bytecode can be run on any computer with the Java interpreter
installed.
10
Variables and Data Types
Variable
• A memory location with a name and a type that stores a value.
• reserved memory locations to store values.
• This means that when you create a variable you reserve some
space in memory.
• Based on the data type of a variable, the operating system
allocates memory and decides what can be stored in the reserved
memory.
11
Variables and Data Types
Data Types
12
Java Reserved Words (Keywords)
• The following keywords are reserved in the Java language.
• They can never be used as identifiers
abstract assert boolean break byte
case catch char class const
continue default do double else
extends final finally float for
return if implements import instanceof
int interface long native new
package private protected public return
short static strictfp super switch
synchronized this throw throws transient
int highScore ;
type name
• Now you have the variable (highScore), you will want to assign a value to it.
14
Naming Variables
• All Java components require names.
• Names used for classes, variables, and methods are
called identifiers.
• In Java, All identifiers should begin with:
• A to Z or a to z
• currency character ($) or
• an underscore ( _ )
• A keyword cannot be used as an identifier.
• Identifiers are case sensitive
• legal identifiers: age, $salary, _value, __1_value.
• illegal identifiers: 123abc, -salary.
15
Types of Variables
• Variables are places where information can be stored while a
program is running.
• There are three kind of variables in Java:
• Local variables: declared in methods, constructors, or blocks.
• Instance variables: declared in a class, but outside a method, constructor
or any block.
Selection Stat.
Iteration Stat.
Jump Statements
18
Java OOP Concepts
Objects
21
Java OOP Concepts
Inheritance
22
(base class, parent class).
Java OOP Concepts
Inheritance
24
Java OOP Concepts
Abstraction
25
Java OOP Concepts
Encapsulation
26 variables values.
Exception Handling
• Exception is a problem that arises during the execution of a
program.
• When an exception occurs, the normal flow of the program will be
interrupted.
• which is not recommended, therefore, these exceptions are to be
handled.
• Exception handling in java is a mechanism to handle runtime
errors that can occur in a program.
• It allows the program to continue execution even when an error
occurs, rather than terminating abnormally.
Exception Handling
A program that does not provide code for catching and handling exceptions will
terminate abnormally, and may cause serious problems.
Example:- if your program attempts to transfer money from a savings account to a
checking account, but because of a runtime error is terminated after the money is drawn
from the savings account and before the money is deposited in the checking account, the
customer will lose money.
• Exceptions occur for various reasons. the:-
• User may enter an invalid input,
28
Exception Types
• A Java exception is an instance of a class derived from Throwable.
• The Throwable class is contained in the java.lang package, and
subclasses of Throwable are contained in various packages.
• Errors related to GUI components are included in the java.awt
package;
• numeric exceptions are included in the java.lang package
• You can create your own exception classes by extending Throwable
or a subclass of Throwable.
29
Exception Types
• Exceptions thrown are instances of the classes shown in this
diagram, or of subclasses of one of these classes.
30
Exception Types
• Based on these, we have three categories of Exceptions:-
• Checked Exception
• Unchceked Exception
• Errors
Example:
Exception Types
Unchecked Exceptions:
• These are not exceptions at all, but problems that arise beyond
the control of the user or the programmer.
• Errors are typically ignored in your code because you can rarely
do anything about an error.
Example:-
• if a stack overflow occurs, an error will arise.
• They are also ignored at the time of compilation.
Exception Handling
• Java's exception-handling model is based on three operations:-
• declaring an exception,
• throwing an exception, and
• catching an exception
35
Exception Handling
• In Java, exception handling is done using
• try catch mechanism
• Multiple catch mechanism
• try catch finally mechanism
• try finally mechanism
• throw
• throws
• Exception handling is done by transferring the execution of a program
to an appropriate exception handler when exception occurs.
36
Part II
37