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

Chapter 1 Advanced Programming Part I

This document provides an introduction and overview of key Java concepts including: - Java is an object-oriented language developed by Sun Microsystems that is portable, robust, and high-performance. - The main Java terminology includes the Java Development Kit (JDK), Java Runtime Environment (JRE), and Java Virtual Machine (JVM). - Key object-oriented programming concepts in Java are objects, classes, inheritance, polymorphism, abstraction, and encapsulation.

Uploaded by

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

Chapter 1 Advanced Programming Part I

This document provides an introduction and overview of key Java concepts including: - Java is an object-oriented language developed by Sun Microsystems that is portable, robust, and high-performance. - The main Java terminology includes the Java Development Kit (JDK), Java Runtime Environment (JRE), and Java Virtual Machine (JVM). - Key object-oriented programming concepts in Java are objects, classes, inheritance, polymorphism, abstraction, and encapsulation.

Uploaded by

Yoomif Tube
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Chapter One

Introduction to Java
(Review)

Mulugeta G.
Introduction
 Java

• is a general-purpose, class-based, object-oriented language


designed for having lesser implementation dependencies.
• Developed by sun microsystems [James Gosling]
• Is a high level, robust, secured and object-oriented programming
language.
• Based on C/C++
• Have a Widespread acceptance
2
Characteristics of Java
 Simple  Secure

 Object-Oriented  Architecture-Neutral

 Distributed  Portable

 Interpreted  High Performance

 Robust  Multithreaded

 Platform Independent  Dynamic

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

• Represent different values to be


stored in the variable.
• In java, there are two types of
data types
• Primitive Data Types
• Non-primitive 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

13 try void violate while


Variable Declaration
• You must declare all variables before they can be used
• The basic form of a variable declaration is:
• Data-type variableName ;
• The type is listed first followed by the name.
• Example: a variable that stores an integer representing the highest score on an exam
could be declared as follows:

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.

• Class/static variables: declared with the static keyword in a class,

but outside a method, constructor or a block.


16
Java String Methods
• String in Java is a class, and thereby it is obvious that it has wide-
ranging methods associated with it.
• some of the frequently used methods allied with the String class.
• charAt()
• toUpperCase()
• compareTo()
• trim()
• equals()
• concat()
• length()
• split()
• replace()
• valueOf()
• toLowerCase()
17
Control Statements
• Control statements alter the flow of the program
• Used to cause the flow of control to advance and branch based on
changes to the state of a program.
• Java control statements are categorized in to three.
Control Statements

Selection Stat.

Iteration Stat.

Jump Statements
18
Java OOP Concepts
 Objects

• A programming entity that contains state (data) and behavior


(methods).
• State: A set of values (internal data) stored in an object.
• Behavior: A set of actions an object can perform, often reporting or
modifying its internal state.
Example 1: Dogs
• States: name, color, breed, and “is hungry?”
• Behaviors: bark, run, and wag tail
19
Java OOP Concepts
 Class

• is a blueprint from which individual objects are created.


• defines a new data type which can be used to create objects of
that type.
• Thus, a class is a template for an object, and an object is an
instance of a class.
• Syntax: public class ClassName {
...
}
20
Java OOP Concepts
 Creating objects
• Objects are created dynamically using the new keyword.
• It’s called instantiating an object.
• Syntax:
class_name object_name=new class_name();
• Object’s created and used to access members of a class
• ObjectName.VariableName;
• ObjectName.MethodName(parameter-list);

21
Java OOP Concepts
 Inheritance

• defined as the process where one class acquires the properties


(methods and fields) of another.
• With the use of inheritance the information is made manageable
in a hierarchical order.
• The class which inherits the properties of other is known as
subclass (derived class, child class) and
• The class whose properties are inherited is known as superclass

22
(base class, parent class).
Java OOP Concepts
 Inheritance

• extends is the keyword used to inherit the properties of a class.


• Syntax: class Super {
.....
.....
}
class Sub extends Super {
.....
.....
}
23
Java OOP Concepts
 Polymorphism

• The ability of an object to take on many forms.


• Any Java object that can pass more than one IS-A test is
considered to be polymorphic.
• In Java, all Java objects are polymorphic since any object will
pass the IS-A test for their own type and for the class Object.
• It is important to know that the only possible way to access an
object is through a reference variable.

24
Java OOP Concepts
 Abstraction

• abstraction is a process of hiding the implementation details from


the user, only the functionality will be provided to the user.
• In other words, the user will have the information on what the
object does instead of how it does it.
• In Java, abstraction is achieved using Abstract classes and
interfaces.

25
Java OOP Concepts
 Encapsulation

• Encapsulation in Java is a mechanism of wrapping the data


(variables) and code acting on the data (methods) together as a
single unit.
• In encapsulation, the variables of a class will be hidden from
other classes, and can be accessed only through the methods.
• To achieve encapsulation in Java
• Declare the variables of a class as private.
• Provide public setter and getter methods to modify and view the

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,

• Program may attempt to open a file that doesn't exist

• Network connection may hang up, or

• Program may attempt to access an out-of-bounds array element.

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

• RuntimeException, Error, and their subclasses are known as


unchecked exceptions.
• All other exceptions are known as checked exceptions:-
• meaning that the compiler forces the programmer to check and
deal with them.
31
Exception Types
 Checked Exceptions:
• an exception that occurs at the compile time, these are also called
as compile time exceptions.
• Can’t simply be ignored at the time of compilation, the
programmer should take care of (handle) these exceptions.

Example:
Exception Types
 Unchecked Exceptions:

• An exception that occurs at the time of execution.


• also called as Runtime Exceptions. These include programming
bugs, such as:-
• logic errors or improper use of an API.
•Runtime exceptions are ignored at the time of compilation.
Example:
• if you have declared an array of size 5 in your program, and trying to call the 6th element
of the array then an ArrayIndexOutOfBoundsExceptionexception occurs.
Exception Types
 Errors:

• 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

Concepts of AWT and Swing

37

You might also like