Sei sulla pagina 1di 44

CE9001/CM101 Java and the Internet

Chapter 1
Introduction to Computers, Programs, and Java
2
Overviews
1.1 Computer Systems
1.2 Computer software
1.3 Computer programming
1.4 Why Java Programming
1.5 Java basics
1.6 A Simple Java Program
1.7 Anatomy of a Java Program
1.8 Programming Errors
Objectives
To understand computer basics, programs, and operating
systems (1.21.4).
To describe the relationship between Java and the World
Wide Web (1.5).
To understand the meaning of Java language specification,
API, JDK, and IDE (1.6).
To write a simple Java program (1.7).
To display output on the console (1.7).
To explain the basic syntax of a Java program (1.7).
Objectives cont
To create, compile, and run Java programs (1.8).
To become familiar with Java programming style and
documentation (1.10).
To explain the differences between syntax errors, runtime
errors, and logic errors (1.11).

5
1.1 Computer Systems
Computer systems = Hardware + Software +
Data + User

Hardware Physical components/devices

Software Computer programs with instructions
that perform a particular task based on the
hardware
6
Computer Hardware
Control
Unit
Main Memory
`
Central Processing Unit
Registers
Arithmetic
Logic Unit
`
`


Input Device


Output Device
Secondary Storage



`
3. Input Device
-mouse, keyboard, etc.

4. Output Device
-printer, monitor, etc.

5. Secondary Storage
- permanent
- floppy disks, hard disks,
CDs, tapes, USB flash
drives, etc.
1. CPU
- interprets and executes instructions
2. Main Memory
- store data and instructions
- 1 byte = 8 bits
7
1.2 Computer Software (Programs)
Utility Programs
(E.g. Editors)
Hardware
Language Translators
(E.g. Assemblers,
Compilers, Interpreters)
Application Software
(E.g. Word Processors,
Database Systems, etc.)
Operating Systems
(E.g. Unix, MS-DOS, Windows 95, etc.)
Application
Software

System Software
8
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.
9
Human language
Add 2
to 3
???
Computer
1.3 What is Programming?
Computer program
A set of instructions that directs the computer's behavior.

Computer programming
The art and science of designing and writing computer programs.

Programming Languages
10
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
11
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



ADDF3 R1, R2, R3

Assembly Source File
Assembler


1101101010011010

Machine Code File

12
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;


13
Translator, Interpreter and Compiler
Compilers
- converts HLL
programs into ML
- translation is off-line,
not at runtime

Interpreters
- translation is on-line
- HLL programs can
be executed directly
NB: compiled code
can be executed faster
14
Popular High-Level Languages

Language Description

Ada

BASIC

C
C++
C#
COBOL
FORTRAN
Java
Pascal
Python
Visual
Basic

Named for Ada Lovelace, who worked on mechanical general-purpose computers. The Ada
language was developed for the Department of Defense and is used mainly in defense projects.
Beginners All-purpose Symbolic Instruction Code. It was designed to be learned and used easily
by beginners.
Developed at Bell Laboratories. C combines the power of an assembly language with the ease of
use and portability of a high-level language.
C++ is an object-oriented language, based on C.
Pronounced C Sharp. It is a hybrid of Java and C++ and was developed by Microsoft.
COmmon Business Oriented Language. Used for business applications.
FORmula TRANslation. Popular for scientific and mathematical applications.
Developed by Sun Microsystems, now part of Oracle. It is widely used for developing platform-
independent Internet applications.
Named for Blaise Pascal, who pioneered calculating machines in the seventeenth century. It is a
simple, structured, general-purpose language primarily for teaching programming.
A simple general-purpose scripting language good for writing short programs.
Visual Basic was developed by Microsoft and it enables the programmers to rapidly develop
graphical user interfaces.

15
Programming Languages
Learning computer programming is just like learning a
new language such as Japanese, Chinese, Malay, etc.
http://en.wikipedia.org/wiki/List_of_Java_keywords
16

Java is object-oriented
Java is simple
Java is robust and readable
Java is portable
Java is secure and distributed
Java is easy to learn
Java has many useful features
1.4 Why Java Programming?
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 a general purpose programming language.
Java is the Internet programming language.
Smartphones And Apps
Android Apps Java
http://developer.android.com/sdk/index.html
iPhone Apps - Objective C

17
Samsung Galaxy S III vs iPhone 5
18
James Gosling, Sun Microsystems
Inc., 1991
Oracle acquired Sun in 2010
Originally a language for
programming embedded
consumer electronic applications
Initially named as Oak. Then
changed to Java after visiting a
local coffee shop
In 1995 Java is a language for
developing Internet applications
Now one of the most important
general-purpose programming
languages
Why OAK?
Because, while trying to think
of a name, James Gosling
looked out of his office
window and saw an oak tree!
1.5 Java Basics A Brief History
JavaScript
JavaScript uses syntax influenced by that of C.
JavaScript copies many names and naming conventions
from Java, but the two languages are otherwise unrelated
and have very different semantics.
is primarily used in the form of client-side JavaScript,
implemented as part of a Web browser in order to give
enhanced user interfaces and dynamic websites.
http://en.wikipedia.org/wiki/JavaScript


19
Appeared in 1994; 19 years ago
Designed by Brendan Eich
Developer
Netscape Communications
Corporation, Mozilla
Foundation
20

Java Applets
Java programs to be downloaded via the Internet
Relatively small programs
Need a web browser
Java Applications
Standalone programs
Generally larger applications
Only slight difference in programming, this
course focuses on Java applications
Applets vs Java Applications
"What Java Do I Need?"
Java syntax is defined in the java language specification,
and the Java library is defined in the Java API (application
program interface).
You must have a copy of the JRE (Java Runtime
Environment) on your system to run Java applications and
applets.
JVM (Java Virtual Machine) is a program that interprets
Java bytecode.
JDK (Java Development Kit) is the software for developing
and running Java programs. To develop Java applications
and applets, you need the JDK, which includes the JRE.

http://www.oracle.com/technetwork/java/javase/downloads/index.html
21
22
JDK Editions
Java Standard Edition (J2SE)
J2SE can be used to develop client-side standalone applications or applets.
Java Enterprise Edition (J2EE)
J2EE can be used to develop server-side applications such as Java servlets, Java
ServerPages, and Java ServerFaces.
Java Micro Edition (J2ME).
J2ME can be used to develop applications for mobile devices such as cell phones.
This course uses J2SE to introduce Java programming.
23
The Java Development Process
Step2: Compile the Java source code
Step3: Run the compiled Java code or class file
Step1: Write the Java source code
24
Java Virtual Machine (JVM)
The Java compiler does not translate Java source code into
machine language directly, it translates source code into
Java byte code
byte code is the input for JVM
JVM is an interpreter that translates each byte code
instruction into a machine language instruction and execute
it
JVM makes the Java byte code highly portable
JVMs are getting faster nowadays


25
1.6 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!");
}
}
26
Step 1:Creating and Editing Using NotePad++
27
Creating, Compiling, and
Running Programs

Source Code
Create/Modify Source Code
Compile Source Code
i.e., javac Welcome.java
Bytecode
Run Byteode
i.e., java Welcome
Result
If compilation errors
If runtime errors or incorrect result
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

Method Welcome()
0 aload_0


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

Saved on the disk
stored on the disk
Source code (developed by the programmer)
Byte code (generated by the compiler for JVM
to read and interpret, not for you to understand)
28
Type JDK Command:
javac Welcome.java

Step 2: To compile your program
where javac is JDK Java compiler.
Compiler translates the source program into Java bytecode.
The compiler saves the bytecode into the file Welcome.class.
29
Step 3: To run the byte code with the Java
interpreter
Type JDK Command : java Welcome
The class file (bytecode) is loaded into memory and interpreted by the Java Virtual
Machine (JVM)

Successful compilation will create the bytecode class file: Welcome.class
30
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Trace a Program Execution
Enter main method
31
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Trace a Program Execution
Execute statement
print a message
to the console
32
1.7 Anatomy of a Java Program
Class name
Main method
Statements
Statement terminator
Reserved words
Comments
Blocks
33
Class Name
Every Java program must have at least one class. Each class
has a name. By convention, class names start with an
uppercase letter. In this example, the class name is Welcome.
34
Main Method
Line 3 defines the main method. In order to run a class, the
class must contain a method named main. The program is
executed from the main method.
35
Statement
A statement represents an action or a sequence of
actions. The statement
System.out.println("Welcome to Java!") in the
program is a statement to display the greeting
"Welcome to Java!.
36
Statement Terminator
Every statement in Java ends with a semicolon (;).
37
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.
abstract continue for new switch
assert
***
default goto
*
package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum
****
instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp
**
volatile
const
*
float native super while
* not used
** added in 1.2
*** added in 1.4
**** added in 5.0
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html
38
Three ways to write comments:

(1) Line comment
// single line comment

(2) Block/paragraph comment
/* multiple line comment
This is the first comment
This is the second comment
*/
/* also can be single line */

(3) javadoc comment
/** generate document*/


Will manly use these
two types of comments




Mainly used for
documentation purpose

Comments
39
Blocks
A pair of braces { } in a program forms a block that groups
components of a program.


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

Class block
Method block
40
Special Symbols

Character Name Description

{}

()

[]

//

" "

;
Opening and closing
braces
Opening and closing
parentheses
Opening and closing
brackets
Double slashes
Opening and closing
quotation marks
Semicolon

Denotes a block to enclose statements.

Used with methods.

Denotes an array.

Precedes a comment line.

Enclosing a string (i.e., sequence of characters).

Marks the end of a statement.

41
1.8 Programming Errors
Syntax Errors
Detected by the compiler
Runtime Errors
Causes the program to abort
Logic Errors
Produces incorrect result
42
Syntax Errors
public class ShowSyntaxErrors {
public static main(String[] args) {
System.out.println("Welcome to Java);
}
}
Are detected by the complier are called syntax errors
or compile errors.
Result from errors in code construction
Mistyping a keyword
Omitting some necessary punctuation
Using an opening brace without a corresponding closing
brace
Usually easy to detect
43
Runtime Errors
public class ShowRuntimeErrors {
public static void main(String[] args) {
System.out.println(1 / 0);
}
}
Are errors that cause a program to terminate
abnormally.
They occur while a program is running if the
environment detects an operation that is impossible to
carry out.
division by zero
incorrect input type
44
Logic Errors
public class ShowLogicErrors {
public static void main(String[] args) {
System.out.println("Celsius 35 is Fahrenheit degree ");
System.out.println((9 / 5) * 35 + 32);
}
}
Occur when a program does not perform the way it
was intended to.
Occur for many different reasons.
Most difficult to detect.

Potrebbero piacerti anche