You are on page 1of 49

Chapter One

Introduction to Computers, Programs, and Java

1
Objectives
To review computer basics and programs

To understand what programming paradigm is?

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 .


2
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 are instructions that tell a


computer what to do.
3
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.
There are hundreds of programming languages, and
they were developed to make the programming
process easier for people.
Programming languages are classified into three
categories. Machine, Assembly and High-Level Language 4
What is Programming Languages?
Machine Language
Machine language is a set of primitive instructions
built into every computer.
The instructions are in the form of binary code, so
you have to enter binary codes for various
instructions.
Program with native machine language is a tedious
process.
Moreover the programs are highly difficult to read
and modify.
For example, to add two numbers, you might write an
instruction in binary like this: 
1101101010011010 5
What is Programming Languages?
Assembly 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 6
What is Programming Languages?
Assembly 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:
ADD 2, 3, result

7
What is Programming Languages?
Assembly Language

Writingcode 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.

8
What is Programming Languages?
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;
9
What is Programming Languages?
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 for this course Java is our focus.
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. 10
What is Programming Languages?
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.

11
What is Programming Languages?
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
12
translated.
What is Programming Paradigm?
Solving a programming problem requires choosing
the right concepts.
All but the smallest toy problems require different
sets of concepts for different parts.
This is why programming languages should support
many paradigms.
A paradigm is a way of doing something (like
programming).
A programming paradigm is a style or “way” of
programming. or
Programming Paradigms is a way to classify,
programming languages, according to styles of
computer programming. 13
What is Programming Paradigm?
Features of various programming languages
determine which programming paradigms they belong
to.
Some programming languages fall into only one
paradigm, while others fall into multiple paradigms.

Each paradigm supports a set of concepts that makes


it the best for a certain kind of problem.

For example, object-oriented programming is best for


problems with a large number of related data
abstractions organized in a hierarchy.
14
Some Major Types of Programming Paradigms
 It is based on commands that
1. Imperative or procedural update variables in storage.
2. Declarative  Control flow is an explicit
3. Logical or Rule based sequence of commands.
4. Functional
5. Object-Oriented  Commands show how the
computation takes place, step by
step.
 Each step affects the global state
of the computation.

 Some programming Languages


used in this paradigm are Fortran,
Pascal, Basic, C etc

15
Some Major Types of Programming Paradigms

1. Imperative or procedural  Control flow in declarative


2. Declarative programming is implicit: the
programmer states only what the
3. Logical or Rule based
result should look like, not how
4. Functional to obtain it.
5. Object-Oriented
 Programs state the result you
want, not how to get it.

16
Some Major Types of Programming Paradigms

1. Imperative or procedural
2. Declarative  Logic Programming is a
programming paradigm based on
3. Logical or Rule based
formal logic.
4. Functional
5. Object-Oriented  It is a set of sentences in logical
form, expressing facts and rules
about some problem domain.

 Major logic programming


language families include
PROLOG, ASP and DATALOG.

17
Some Major Types of Programming Paradigms

1. Imperative or procedural
2. Declarative  In this paradigm we express
computations as the evaluation of
3. Logical or Rule based
mathematical functions.
4. Functional
5. Object-Oriented  The reason is that the paradigm
originates from a purely
mathematical discipline: the theory
of functions.

 In functional programming control


flow is expressed by combining
function calls, rather than by
assigning values to variables.

18
Some Major Types of Programming Paradigms
 The object-oriented paradigm has
got great popularity in the recent
1. Imperative or procedural years.
2. Declarative
3. Logical or Rule based  Object-Oriented Programming is a
programming paradigm based on
4. Functional
the concept of “Objects”, which may
5. Object-Oriented contain data, in the form of fields,
often known as Attributes; and
code, in the form of Methods.

 These properties are very important


when programs become larger and
larger.

 Java, C++, C#, python are some


examples of object-oriented
programming languages 19
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. 20
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.

21
Some Uses of Java Programming Language
 An application is a program
Java can be used to develop: that runs on your computer,
under the operating system
a) Applications of that computer. An
application created by Java is
more or less like one created
using any other type of
 Java is a general purpose computer language, such as
programming language. 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
applications for small hand-held member of their class
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.
22
Java can be used to develop applications for hand-held and wireless
devices, such as a BlackBerry, PDA and Cell Phone.

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

Java initially became attractive  Although any computer


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

25
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…

26
Key Features of Java Technology
No language is simple, but Java is
a bit easier than the popular
Java Is Simple 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 Robust Java is partially modeled on C++,
Java Is Secure but greatly simplified and
improved.
Java Is Architecture-Neutral
Java Is Portable
Java is Performance
Java Is Multithreaded
Java Is Dynamic

27
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
Java Is Interpreted start to be object-oriented.
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
One of the central issues in software
Java is Performance 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.
28
Key Features of Java …

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

29
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.
Java Is Secure The bytecode is machine-independent
Java Is Architecture-Neutral and can run on any machine that has a
Java Is Portable Java interpreter, which is part of the
Java is Performance Java Virtual Machine (JVM).
Java Is Multithreaded
Java Is Dynamic

30
Key Features of Java …

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

31
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 is Performance
Java Is Multithreaded
Java Is Dynamic

32
Key Features of Java …

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

33
Key Features of Java …

Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Because Java is architecture
Java Is Interpreted
neutral, Java programs are
Java Is Robust portable.
Java Is Secure
Java Is Architecture-Neutral They can be run on any platform
Java Is Portable without being recompiled.
Java is Performance
There is no platform-specific
Java Is Multithreaded features in the Java language.
Java Is Dynamic

34
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 is 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.

35
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 is Performance Multithread programming is smoothly
Java Is Multithreaded integrated in Java, whereas in other
languages you have to call procedures
Java Is Dynamic specific to the operating system to enable
multithreading.

36
Key Features of Java …

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

37
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.
o The Java language specification is a technical definition
of the language that includes the syntax and semantics
of the Java programming language.
o The application program interface (API) contains
predefined classes and interfaces for developing Java
programs.
NB: The following picture shows the Java API specification.
38
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

39
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 Java Server Pages.
• J2ME can be used to develop applications for mobile
devices, such as cell phones.
40
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.
o Java Platform = JDK (because JRE is subset of JDK )
o JRE = 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
41
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.

42
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
43
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!");
}
}
A class is a template used to create an object.
Class is a named template or descriptor for a set of objects
that share the same attributes(data Field),
behaviors(methods), and relationships.
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. 44
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. 45
Creating, Compiling and Executing ...

The following figure describes the process of creating,


compiling and executing a Java program.

46
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

47
If runtime errors or incorrect result
Java Environment/Life Cycle of Java Code

Runtime
Compile-time Environment Class
Environment Java
Loader
Class
Bytecode Libraries
Java Verifier
Source
(.java)

Just in
Java
Time
Interpreter Java
Java Compiler
Bytecodes Virtual
Java move locally machine
Compiler or through
network
Runtime System

Java
Bytecod Operating System
e
(.class )
Hardware
Question Answer

END !!

49

You might also like