0% found this document useful (0 votes)
10 views49 pages

Introduction JP

The document provides an overview of computer programs, programming languages, and their classifications, including machine language, assembly language, and high-level languages. It emphasizes the role of Java as a versatile programming language for developing applications across various platforms, particularly on the Internet. Additionally, it discusses the characteristics of Java, the process of compiling and running Java programs, and the anatomy of a Java program.

Uploaded by

Faizan Sait
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views49 pages

Introduction JP

The document provides an overview of computer programs, programming languages, and their classifications, including machine language, assembly language, and high-level languages. It emphasizes the role of Java as a versatile programming language for developing applications across various platforms, particularly on the Internet. Additionally, it discusses the characteristics of Java, the process of compiling and running Java programs, and the anatomy of a Java program.

Uploaded by

Faizan Sait
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Programs

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.

1
Programming Languages
Machine Language Assembly Language High-Level 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
2
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.
For example, to add two numbers, you might write an
instruction in assembly code like this:
ADDF3 R1, R2, R3
Assembly Source File
Machine Code File


ADDF3 R1, R2, R3
Assembler …
1101101010011010
… …

3
Programming Languages
Machine Language Assembly Language High-Level Language

The high-level languages are English-like and easy to learn


and program. 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;

4
Popular High-Level Languages
!COBOL (COmmon Business Oriented Language)
!FORTRAN (FORmula TRANslation)
!BASIC (Beginner All-purpose Symbolic Instructional Code)
!Pascal (named for Blaise Pascal)
!Ada (named for Ada Lovelace)
!C (whose developer designed B first)
!Visual Basic (Basic-like visual language developed by Microsoft)
!Delphi (Pascal-like visual language developed by Borland)
!C++ (an object-oriented language, based on C)
!C# (a Java-like language developed by Microsoft)
!Java (We use it in this course)

5
Compiling Source Code
A program written in a high-level language is called a
source program. Since a computer cannot understand a
source program. Program called a compiler is used to
translate the source program into a machine language
program called an object program. The object program is
often then linked with other supporting library code before
the object can be executed on the machine.

Source File Compiler Machine-language


Linker Executable File
File

Library Code

6
Operating Systems
The operating system (OS) is
a program that manages and User
controls a computer’s
activities. You are probably Application Programs
using Windows 98, NT, 2000,
XP, ME, 7 or 8. Windows is Operating System
currently the most popular PC
operating system. Application
Hardware
programs such as an Internet
browser and a word processor
cannot run without an
operating system.
7
Why Java?
The answer is that 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. Java
is the Internet programming language.

!Java is a general purpose programming language.


!Java is the Internet programming language.

8
Java, Web, and Beyond
! Java can be used to develop Web
applications.
! Java Applets
! Java Web Applications
! Java can also be used to develop applications
for hand-held devices such as Palm and cell
phones

9
Examples of Java’s Versatility (Applets)

10
Java’s History
! James Gosling and Sun Microsystems
! Oak
! Java, May 20, 1995, Sun World
! HotJava
– The first Java-enabled Web browser
! Early History Website:
http://java.sun.com/features/1998/05/birthday.html

11
Companion
Website Characteristics 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 Is Portable
! Java's Performance
! Java Is Multithreaded
! Java Is Dynamic
www.cs.armstrong.edu/liang/intro8e/JavaCharacteristics.pdf
12
Companion
Website Characteristics of Java
! Java Is Simple Java is partially modeled on C++, but greatly
simplified and improved. Some people refer to
! Java Is Object-Oriented Java as "C++--" because it is like C++ but
! Java Is Distributed with more functionality and fewer negative
aspects.
! 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

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

14
Companion
Website Characteristics of Java
! Java Is Simple Distributed computing involves several
computers working together on a network.
! Java Is Object-Oriented Java is designed to make distributed
! Java Is Distributed computing easy. Since networking
capability is inherently integrated into
! Java Is Interpreted
Java, writing network programs is like
! Java Is Robust sending and receiving data to and from a
! Java Is Secure file.

! Java Is Architecture-Neutral
! Java Is Portable
! Java's Performance
! Java Is Multithreaded
! Java Is Dynamic

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

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

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

18
Companion
Website Characteristics 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 Write once, run anywhere
! Java Is Portable With a Java Virtual Machine (JVM),
! Java's Performance you can write one program that will
run on any platform.
! Java Is Multithreaded
! Java Is Dynamic

19
Companion
Website Characteristics 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 Is Portable Because Java is architecture neutral,
Java programs are portable. They can
! Java's Performance be run on any platform without being
! Java Is Multithreaded recompiled.
! Java Is Dynamic

20
Companion
Website Characteristics 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 Is Portable Java’s performance Because Java is
architecture neutral, Java programs are
! Java's Performance portable. They can be run on any
! Java Is Multithreaded platform without being recompiled.
! Java Is Dynamic

21
Companion
Website Characteristics 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 Is Portable
! Java's 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.
22
Companion
Website Characteristics 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 Is Portable
! Java's Performance Java was designed to adapt to an evolving
environment. New code can be loaded on the
! Java Is Multithreaded fly without recompilation. There is no need for
developers to create, and for users to install,
! Java Is Dynamic major new software versions. New features can
be incorporated transparently as needed.

23
Popular Java IDEs
! NetBeans Open Source by Sun
! Eclipse Open Source by IBM

24
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!");
}
}

Welcome

Run

25
Creating and Editing Using NotePad
To use NotePad, type
notepad Welcome.java
from the DOS prompt.

26
Creating and Editing Using WordPad
To use WordPad, type
write Welcome.java
from the DOS prompt.

27
Creating, Compiling, and
Running Programs
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

If runtime errors or incorrect result 28


Compiling Java Source Code
You can port a source program to any machine with appropriate
compilers. The source program must be recompiled, however, because
the object program can only run on a specific machine. Nowadays
computers are networked to work together. Java was designed to run
object programs on any platform. With Java, you write the program
once, and compile the source program into a special type of object
code, known as bytecode. The bytecode can then run on any computer
with a Java Virtual Machine, as shown below. Java Virtual Machine is
a software that interprets Java bytecode.
Java Bytecode

Java Virtual
Machine

Any
Computer

29
animation

Trace a Program Execution


Enter main method

//This program prints Welcome to Java!


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

30
animation

Trace a Program Execution


Execute statement

//This program prints Welcome to Java!


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

31
animation

Trace a Program Execution

//This program prints Welcome to Java!


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

print a message to the


console

32
Two More Simple Examples

Welcome1 Run

ComputeExpression Run

33
Companion
Website Compiling and Running Java
from the Command Window
! Set path to JDK bin directory
– set path=c:\Program Files\java\jdk1.6.0\bin
! Set classpath to include the current directory
– set classpath=.
! Compile
– javac Welcome.java
! Run
– java Welcome
35
Compiling and Running Java
Companion
Website from TextPad
! See Supplement II.A on the Website for details

36
Companion
Website Compiling and Running
Java from NetBeans
! Demo

38
Anatomy of a Java Program
! Comments
! Reserved words
! Modifiers
! Statements
! Blocks
! Classes
! Methods
! The main method
39
Comments
Three types of comments in Java.

Line comment: A line comment is preceded by two


slashes (//) in a line.
Paragraph comment: A paragraph comment is enclosed
between /* and */ in one or multiple lines.

javadoc comment: javadoc comments begin with /**


and end with */. They are used for documenting
classes, data, and methods. They can be extracted into
an HTML file using JDK's javadoc command.
40
Reserved Words
Reserved words or keywords are words that have a
specific meaning to the compiler and cannot be used for
other purposes in the program. For example, when the
compiler sees the word class, it understands that the word
after class is the name for the class. Other reserved words
in Listing 1.1 are public, static, and void. Their use will
be introduced later in the book.

41
Modifiers
Java uses certain reserved words called modifiers that
specify the properties of the data, methods, and
classes and how they can be used. Examples of
modifiers are public and static. Other modifiers are
private, final, abstract, and protected. A public datum,
method, or class can be accessed by other programs.
A private datum or method cannot be accessed by
other programs. Modifiers are discussed in Chapter 6,
“Objects and Classes.”

42
Statements
A statement represents an action or a sequence of
actions. The statement System.out.println("Welcome to
Java!") in the program in Listing 1.1 is a statement to
display the greeting "Welcome to Java!" Every statement
in Java ends with a semicolon (;).

43
Blocks
A pair of braces in a program forms a block that groups
components of a program.

public class Test {


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

44
Classes
The class is the essential Java construct. A class is a
template or blueprint for objects. To program in Java,
you must understand classes and be able to write and use
them. The mystery of the class will continue to be
unveiled throughout this course. For now, though,
understand that a program is defined by using one or
more classes.

45
Methods
What is System.out.println? It is a method: a collection
of statements that performs a sequence of operations to
display a message on the console. It can be used even
without fully understanding the details of how it works.
It is used by invoking a statement with a string argument.
The string argument is enclosed within parentheses. In
this case, the argument is "Welcome to Java!" You can
call the same println method with a different argument to
print a different message.

46
main Method
The main method provides the control of program flow.
The Java interpreter executes the application by invoking
the main method.

The main method looks like this:

public static void main(String[] args) {


// Statements;
}

47
Summary…
! Program
! Programming Languages
– Machine
– Assembly
– High level

48
Summary…
! Operating System
! Compiler
! Why Java?
! Java History
! JDK versions
! Java Characteristics
! Popular Java IDE

49
Summary
! JVM
! Compiling and Running Java Program
– Using notepad , javac and java commands
– Using Net beans IDE
! Anatomy of Java Program
– Comments
– Reserved Words
– Modifiers
– Statement
– Block
– Class
– Methods

! Output Method (println)


50
Book Source
! Daniel Liang, Introduction to Java
Programming, Eighth Edition, (c) 2011
Pearson Education

51

You might also like