Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 45

Object Oriented Programming -Java

By Hagos Tesfahun

Computing Faculty
Institute of Technology
Bahir Dar University
2008 E.C

1
Chapter One
Introduction to Computers, Programs, and Java

2
Objectives

To review computer basics and programs

To distinguish the terms API, IDE, and JDK.

To write a simple Java program .

To display output on the console .

To explain the basic syntax of a Java program .

To create, compile, and run Java programs .


3
What is a Computer?

 A computer is an electronic device that takes an


input, process it under a set of instructions called
program and produce an output in the need format.

It has different components which help it to accept


input, to store the input, to process the input and to
display the output to the user.

4
What is Computer Program?
Computer programs, known as software, are
instructions to the computer.
You tell a computer what to do through programs.
Without programs, a computer is an empty machine.

 Computers do not understand human languages, so


you need to use computer languages to communicate
with them.
Programs are written using programming languages.

Computer programs, known as software, are


instructions that tell a computer what to do.
5
What is Programming Languages?

Programming languages are languages that


programmers use to write code/program to instruct a
computer.

They are languages designed for programming a


computer.

Computers do not understand human languages, so


programs must be written in a language a computer
can use.
6
What is Programming Languages?

There are hundreds of programming languages, and


they were developed to make the programming
process easier for people.

However, all programs must be converted into a


language the computer can understand.

Programming languages are classified into three


categories.

7
What is Programming Languages?
Machine Language Assembly Language High-Level Language

8
What is Programming Languages?
Machine Language Assembly Language High-Level Language

Programming in machine language is a tedious process.


Moreover, programs written in machine language are very
difficult to read and modify.
For this reason, assembly language was created in the early
days of computing as an alternative to machine languages.
 Assembly language uses a short descriptive word, known as a
mnemonic, to represent each of the machine-language
instructions.
For example, the mnemonic add typically means to add
numbers and sub means to subtract numbers.
To add the numbers 2 and 3 and get the result, you might
write an instruction in assembly code like this: add 2, 3, result
9
What is Programming Languages?
Machine Language Assembly Language High-Level Language
Assembly languages were developed to make programming
easy.
Since the computer cannot understand assembly language,
however, a program called assembler is used to convert
assembly language programs into machine code, as shown in the
following Figure.
For example, to add two numbers, you might write an
instruction in assembly code like this:
ADDF3 R1, R2, R3

10
What is Programming Languages?
Machine Language Assembly Language High-Level Language

Writing code in assembly language is easier than in machine


language.
However, it is still tedious to write code in assembly language.

Writing in assembly requires that you know how the CPU


works.

Assembly language is referred to as a low-level language,


because assembly language is close in nature to machine
language and is machine dependent.

11
What is Programming Languages?
Machine Language Assembly Language High-Level Language
They are platform-independent, which means that
you can write a program in a high level language and
run it in different types of machines.
The high-level languages are English-like and easy to
learn and program.
The instructions in a high-level programming
language are called statements.
For example, the following is a high-level language
statement that computes the area of a circle with
radius 5: area = 5 * 5 * 3.1415;
12
What is Programming Languages?
Machine Language Assembly Language High-Level Language

There are many high-level programming languages,


and each was designed for a specific purpose.
Some popular ones are C, C++, C#, VIB and Java.
But Java is the focus of this course.
A program written in a high-level language is called a
source program or source code.
Because a computer cannot understand a source
program, a source program must be translated into
machine code for execution.
13
What is Programming Languages?
Machine Language Assembly Language High-Level Language

The translation can be done using another


programming tool called an interpreter or a compiler.
 An interpreter reads one statement from the source
code, translates it to the machine code or virtual
machine code, and then executes it right away, as
shown in the following figure.

14
What is Programming Languages?
Machine Language Assembly Language High-Level Language

A compiler translates the entire source code into a


machine-code file, and the machine-code file is then
executed, as shown in figure below.

The difference between compiling and interpreting is that


compiling translates the high-level code into a target language
code as a single unit and
Interpreting translates the individual steps in a high-level
program one at a time rather than the whole program as a single
unit. Each step is executed immediately after it is translated. 15
What is Java Programming Language?
Java is a powerful and versatile programming
language for developing software running on mobile
devices, desktop computers, and servers.
Java programming language is an Object-Oriented
general purpose programming language that has a
syntax similar to that of C/C++.
It is designed to be powerful and simple by avoiding
the overly complex features that have bogged down
other OOLs, such as C++.
As the result, the java language has proved very
much popular with programmers.
16
What is Java Programming Language?

Java enables users to develop and deploy


applications on the Internet for servers, desktop
computers, and small hand-held devices.

The future of computing is being profoundly


influenced by the Internet, and Java promises to
remain a big part of that future.

Hence, Java is the Internet programming language.

17
Some Uses of Java Programming Language
An application is a program
Java can be used to develop: that runs on your computer,
a) Applications under the operating system of
that computer. An application
created by Java is more or less
like one created using any other
 Java is a general purpose type of computer language,
programming language. such as Visual Basic or C++.
 You can use it to develop applications Java applications do
on your desktop and on the server. have the main() as
 You can also use it to develop member of their class
applications for small hand-held
devices, such as personal digital
assistants (PDA) and cell phones.
E.g., The following pictures shows a Java
program that displays the calendar on a
BlackBerry® and on a cell phone.
18
Java can be used to develop applications for hand-held and wireless
devices, such as a BlackBerry, PDA and Cell Phone.

19
Some Uses of Java …
An applet is an application
b) Java applets designed to be transmitted over
the Internet and executed by a
Java-compatible Web browser.

 Although any computer


Java initially became attractive language can be used to create
because Java programs can be run an application, only Java can be
from a Web browser. used to create an applet.
Java programs that run from a
Web browser are called applets. The reason is that Java solves
Applets use a modern graphical two of the thorniest problems
user interface with buttons, text associated with applets: security
fields, text areas, radio buttons, and and portability.
so on, to interact with users on the
Web and process their requests.
 Applets make the Web
responsive, interactive, and fun to
use. 20
Some Uses of Java …
E.g., an applet running from a Web browser.

21
Some Uses of Java …

c) Server-side applications
Java server pages (JSP)
Java Servlets
Java Server Face (JSF)
Java Beans, etc.
d) Multimedia application
Java Graphics, Java Graphics 2D, etc.
e) Etc…

22
Key Features of Java Technology
No language is simple, but Java is
Java Is Simple a bit easier than the popular
object-oriented programming
Java Is Object-Oriented language C++, which was the
Java Is Distributed dominant software-development
Java Is Interpreted language before Java. Java is
Java Is Robust partially modeled on C++, but
Java Is Secure greatly simplified and improved.
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic

23
Key Features of Java …
Java is inherently object-oriented.
Java Is Simple Although many object-oriented
Java Is Object-Oriented languages began strictly as procedural
Java Is Distributed languages, Java was designed from the
start to be object-oriented.
Java Is Interpreted
Java Is Robust Object-oriented programming (OOP) is
Java Is Secure a popular programming approach that
is replacing traditional procedural
Java Is Architecture-Neutral programming techniques.
Java Is Portable
Java's Performance One of the central issues in software
development is how to reuse code.
Java Is Multithreaded Object-oriented programming provides
Java Is Dynamic great flexibility, modularity, clarity, and
reusability through encapsulation,
inheritance, and polymorphism.
24
Key Features of Java …

Java Is Simple
Java Is Object-Oriented
Java Is Distributed Distributed computing involves several
Java Is Interpreted computers working together on a
Java Is Robust network. Java is designed to make
distributed computing easy. Since
Java Is Secure networking capability is inherently
Java Is Architecture-Neutral integrated into Java, writing network
Java Is Portable programs is like sending and receiving
Java's Performance data to and from a file.
Java Is Multithreaded
Java Is Dynamic

25
Key Features of Java …

Java Is Simple
Java Is Object-Oriented
You need an interpreter to run Java
Java Is Distributed
programs. The programs are compiled
Java Is Interpreted into the Java Virtual Machine code
Java Is Robust called bytecode. The bytecode is
Java Is Secure machine-independent and can run on
Java Is Architecture-Neutral any machine that has a Java interpreter,
which is part of the Java Virtual
Java Is Portable
Machine (JVM).
Java's Performance
Java Is Multithreaded
Java Is Dynamic

26
Key Features of Java …

Java Is Simple
Java compilers can detect many
Java Is Object-Oriented problems that would first show up at
Java Is Distributed execution time in other languages.
Java Is Interpreted
Java Is Robust Java has eliminated certain types of
error-prone programming constructs
Java Is Secure
found in other languages.
Java Is Architecture-Neutral
Java Is Portable Java has a runtime exception-handling
Java's Performance feature to provide programming
Java Is Multithreaded support for robustness.
Java Is Dynamic

27
Key Features of Java …

Java Is Simple
Java Is Object-Oriented Java implements several security
Java Is Distributed mechanisms to protect your system
against harm caused by stray programs.
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic

28
Key Features of Java …

Java Is Simple
Java Is Object-Oriented Architectural-Neutral means
Java Is Distributed platform-independent.
Java Is Interpreted
Java Is Robust With a Java Virtual Machine
(JVM), you can write one program
Java Is Secure that will run on any platform.
Java Is Architecture-Neutral
Java Is Portable With a Java Virtual Machine
Java's Performance (JVM), you can write one program
Java Is Multithreaded that will run on any platform.
Java Is Dynamic

29
Key Features of Java …

Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Because Java is architecture
Java Is Robust neutral, Java programs are
Java Is Secure portable. They can be run on any
Java Is Architecture-Neutral platform without being
Java Is Portable recompiled.
Java's Performance
Java Is Multithreaded
Java Is Dynamic

30
Key Features of Java …

Java’s performance is sometimes


Java Is Simple criticized.
Java Is Object-Oriented
Java Is Distributed Because Java is architecture neutral,
Java Is Interpreted Java programs are portable. They
can be run on any platform without
Java Is Robust being recompiled.
Java Is Secure
Java Is Architecture-Neutral Java developers developed the Java
Java Is Portable HotSpot Performance Engine, which
Java's Performance includes a compiler for optimizing
the frequently used code.
Java Is Multithreaded The HotSpot Performance Engine
Java Is Dynamic can be plugged into a JVM to
dramatically boost its performance.

31
Key Features of Java …

Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure Multithreading is a program’s capability
Java Is Architecture-Neutral to perform several tasks
simultaneously.
Java Is Portable
Java's Performance Multithread programming is smoothly
Java Is Multithreaded integrated in Java, whereas in other
Java Is Dynamic languages you have to call procedures
specific to the operating system to
enable multithreading.
32
Key Features of Java …

Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral Java was designed to adapt to an
Java Is Portable evolving environment. New code can be
Java's Performance loaded on the fly without
Java Is Multithreaded recompilation. There is no need for
developers to create, and for users to
Java Is Dynamic install, major new software versions.
New features can be incorporated
transparently as needed.
33
The Java Language Specification, API, JDK, and IDE

Computer languages have strict rules of usage. If you do not


follow the rules when writing a program, the computer will be
unable to understand it.
 Sun Microsystems, the originator of Java, intends to retain
control the language from losing its unified standards:
The Java language specification and Java API define the
Java standard.
oThe Java language specification is a technical
definition of the language that includes the syntax and
semantics of the Java programming language.
oThe application program interface (API) contains
predefined classes and interfaces for developing Java
programs.
NB: The following picture shows the Java API specification.
34
The Java Language Specification…
Java API Specification
Version Number of Packages Number of classes
Java 1.0 8 212
Java 1.1 23 504
Java 1.2 59 1520
Java 1.3 77 1595
Java 1.4 103 2175
Java 1.5 131 2656
Java 1.6 203 3792
Java 1.7 209 4024

35
The Java Language Specification…

There are three editions of the Java JDK:


Java 2 Standard Edition (J2SE)
Java 2 Enterprise Edition (J2EE)
Java 2 Micro Edition (J2ME).

Java is a full-fledged and powerful language that can be used


in many ways.
• J2SE can be used to develop client-side standalone
applications or applets.
• J2EE can be used to develop server-side applications,
such as Java servlets and JavaServer Pages.
• J2ME can be used to develop applications for mobile
devices, such as cell phones.
36
The Java Language Specification…
The two principal products in the Java platform are: Java
Development Kit (JDK) and Java Runtime Environment (JRE).

The JDK is a superset of the JRE, and contains everything


that is in the JRE, plus tools such as the compilers and
debuggers necessary for developing applets and
applications.
oJava Platform = JDK (because JRE is subset of JDK )
oJRE = JVM + Java Packages of Classes(like util, math,
lang, awt, swing etc)+ runtime libraries.

NB:
If you have the JDK installed, you don’t need to install
the JRE separately to run any Java software.
JVM is platform dependent
37
The Java Language Specification…
Integrated Development Environment (IDE) is provided by the
Java development tool for rapidly developing Java programs.

Editing, compiling, debugging, and online help are integrated in


one graphical user interface.

Just enter source code in one window or open an existing


file in a window, then click a button, menu item, or function
key to compile and run the program.

38
Java Development Tools

Three major development tools are:


NetBeans Open Source by Sun
Eclipse Open Source by IBM
JBuilder by Borland

Other useful tools are:


Code Warrior by Metrowerks
TextPad Editor
JCreator LE
JEdit
JGrasp
BlueJ
DrJava
39
A Simple Java Program

//This program prints Welcome to Java!

public class Welcome {


public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

Every Java program must have at least one class.


A class is a construct that defines data and methods.

Each class has a name.


By convention, class names start with an uppercase letter.

40
Creating, Compiling, and Executing a Java Program

The Java programming-development process consists of


creating/modifying source code, compiling, and executing
programs.
Like any other programming language, Java has its own
syntax, and you need to write code that obeys the syntax rules.
 The Java compiler will report syntax errors if your program
violates the syntax rules.
If there are no syntax errors, the compiler generates a
bytecode file with a .class extension.
The bytecode is similar to machine instructions but is
architecture-neutral and can run on any platform that has a
JVM. This is one of Java's primary advantages: Java bytecode
can run on a variety of hardware platforms and operating
systems.
41
Creating, Compiling and Executing ...

The following figure describes the process of creating,


compiling and executing a Java program.

42
Creating, Compiling, and Executing ...

Create/Modify Source Code

Source code (developed by the programmer)


Saved on the disk
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!"); Source Code
}
}

Compile Source Code


Byte code (generated by the compiler for JVM i.e., javac Welcome.java
to read and interpret, not for you to understand)

Method Welcome() If compilation errors
0 aload_0 stored on the disk

Bytecode
Method void main(java.lang.String[])
0 getstatic #2 …
3 ldc #3 <String "Welcome to
Java!">
5 invokevirtual #4 …
8 return Run Byteode
i.e., java Welcome

Result

43
If runtime errors or incorrect result
Creating, Compiling, and Executing ...

44
Questio Answer
n

45

You might also like