Lecture 1 An Introduction to Computer, Programming and Java 1.

45
Lecture 1 An Introduction to Computer, Programming and Java 1

Transcript of Lecture 1 An Introduction to Computer, Programming and Java 1.

Page 1: Lecture 1 An Introduction to Computer, Programming and Java 1.

1

Lecture 1

An Introduction to Computer, Programming and Java

Page 2: Lecture 1 An Introduction to Computer, Programming and Java 1.

2

What is a Computer?A computer is a machine that performs computations, logical operations, or more generally, data manipulation according to some prescribed sequence of instructions called a computer program.

The physical components of a computer are termed hardware and the programs software.

Page 3: Lecture 1 An Introduction to Computer, Programming and Java 1.

3

The HardwareThe major hardware components:

The central processing unit (CPU) Primary or random access memory (RAM) Secondary or long term memory Input and output devices (I/O devices)

Page 4: Lecture 1 An Introduction to Computer, Programming and Java 1.

4

The Central Processing UnitThe CPU does: the computing the processing the majority of the work Important components of the CPU: arithmetic and logic unit (ALU) control unit (CU) clock

Page 5: Lecture 1 An Introduction to Computer, Programming and Java 1.

5

The Central Processing Unit The ALU performs calculations, billions per

second The CU controls or coordinates which

calculations the ALU performs The CPU clock determines how frequently the

computer hardware executes instructions. A system’s hardware components are

synchronized with the clock. Every time the clock ticks, another hardware action occurs.

Page 6: Lecture 1 An Introduction to Computer, Programming and Java 1.

6

Primary or Random Access MemoryHow Data is Stored binary format a sequence of 0’s and 1's called bits.

ASCII encoding: ‘a’ is represented by 01100001 ‘b’ is encoded as 01100010

A sequence of eight bits is called a byte.

Page 7: Lecture 1 An Introduction to Computer, Programming and Java 1.

7

Where Data is Stored When the CPU executes a program, the

program instructions, along with relevant data, are stored in primary memory.

Primary memory is also known as random access memory (RAM) because data may be retrieved or accessed in random, rather than sequential, order.

Page 8: Lecture 1 An Introduction to Computer, Programming and Java 1.

8

Where Data Is Stored You can conceptualize RAM as a collection of

storage cells or boxes, each capable of holding just a single byte of information.

A unique number, or memory address, identifies each such storage cell.

Page 9: Lecture 1 An Introduction to Computer, Programming and Java 1.

9

Secondary Memory Long term Permanent storage

Secondary memory devices: hard disks tapes CDs flash memory sticks.

Page 10: Lecture 1 An Introduction to Computer, Programming and Java 1.

10

Secondary Memory The programs that you use every day such as

word processors, spreadsheets, and games are permanently stored on secondary storage devices.

Compared to RAM, secondary memory is, in general, cheaper (per bit), slower, larger, electromechanical rather than electronic, and persistent: secondary memory devices do not lose their values when you turn off the computer.

Page 11: Lecture 1 An Introduction to Computer, Programming and Java 1.

11

Input/Output Devices Standard input devices:

keyboards mouses joysticks stylus pens cameras microphones

Output devices:

monitors printers speakers

Page 12: Lecture 1 An Introduction to Computer, Programming and Java 1.

12

The Software The programs that run on a computer are

collectively known as software. Word processors, internet browsers, editors, database management systems, computer games, and spreadsheets are all part of your computer's software library.

When you turn on or boot your computer, a program called the operating system automatically runs. This special program provides an interface between you and your computer.

Page 13: Lecture 1 An Introduction to Computer, Programming and Java 1.

13

Programming LanguagesMachine 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

Page 14: Lecture 1 An Introduction to Computer, Programming and Java 1.

14

Programming Languages

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

Page 15: Lecture 1 An Introduction to Computer, Programming and Java 1.

15

Programming Languages

The high-level languages are English-like and easy to learn and program. For example, the following is a high-level language statement that adds two numbers:

total = 3 + 4;

Page 16: Lecture 1 An Introduction to Computer, Programming and Java 1.

16

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 the book)

Page 17: Lecture 1 An Introduction to Computer, Programming and Java 1.

17

Programming and Algorithms An algorithm is a finite, step-by-step

procedure for accomplishing some task or solving a problem.

The study of algorithms is a basis of computer science.

A programming language is your tool, a tool that you can use to investigate and implement algorithms.

Page 18: Lecture 1 An Introduction to Computer, Programming and Java 1.

18

Compiling Source CodeA program written in a high-level language is called a source program. 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.

Compiler Source File Machine-language

File Linker Executable File

Library Code

Page 19: Lecture 1 An Introduction to Computer, Programming and Java 1.

The Compiler The program must be translated into the

machine language of that computer.

Accepts a program written in a high-level language and produces a translation into the target machine language.

Page 20: Lecture 1 An Introduction to Computer, Programming and Java 1.

20

Java General-purpose language developed by Sun

Microsystems in the early 1990s, which became part of Oracle Corporation in 2010. Three main goals for their new language:

Platform Independence - Java programs should be capable of running on any computer.

Security - Java programs should not be susceptible/open to hackers' code and dangerous viruses.

Reliability - Java programs should not "crash.”

Page 21: Lecture 1 An Introduction to Computer, Programming and Java 1.

21

The Java Virtual Machine In order to make Java a cross-platform

programming language, Java's creative team designed an abstract computer implemented in software called the Java Virtual Machine (JVM). You install software on your computer that simulates a JVM computer. The machine language of the JVM is called bytecode. Java programs are first compiled into bytecode, and then executed.

Page 22: Lecture 1 An Introduction to Computer, Programming and Java 1.

The Java Virtual Machine The Java interpreter, which is part of the JVM,

executes each bytecode instruction, one by one.

Once a Java program is translated into bytecode, the bytecode can run on any computer that has installed the JVM. A Java program needs to be compiled into bytecode just once.

Page 23: Lecture 1 An Introduction to Computer, Programming and Java 1.

23

Java, Web, and Beyond Java can be used to develop any

applications. Java Applets Java Web Applications Java can also be used to develop

applications for hand-held devices such as Palm, cell phones, PC Tablets, etc.

Page 24: Lecture 1 An Introduction to Computer, Programming and Java 1.

24

PDA and Cell Phone

Page 25: Lecture 1 An Introduction to Computer, Programming and Java 1.

25

Java’s History James Gosling and Sun Microsystems Oak HotJava

The first Java-enabled Web browser

Bought by Oracle Corporation in January 2010 Java History Website:

http://en.wikipedia.org/wiki/Java_(programming_language)

Page 26: Lecture 1 An Introduction to Computer, Programming and Java 1.

Characteristics of Java

26

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

http://www.cs.armstrong.edu/liang/intro8e/JavaCharacteristics.pdf

Page 27: Lecture 1 An Introduction to Computer, Programming and Java 1.

27

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

Java is partially modeled on C++, but greatly simplified and improved. Some people refer to Java as "C++--" because it is like C++ but with more functionality and fewer negative aspects.

Page 28: Lecture 1 An Introduction to Computer, Programming and Java 1.

28

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

Java is inherently object-oriented. Although many object-oriented languages began strictly as procedural languages, Java was designed from the start to be object-oriented. Object-oriented programming (OOP) is a popular programming approach that is replacing traditional procedural programming techniques.

One of the central issues in software development is how to reuse code. Object-oriented programming provides great flexibility, modularity, clarity, and reusability through encapsulation, inheritance, and polymorphism.

Page 29: Lecture 1 An Introduction to Computer, Programming and Java 1.

29

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

Distributed computing involves several computers working together on a network. Java is designed to make distributed computing easy. Since networking capability is inherently integrated into Java, writing network programs is like sending and receiving data to and from a file.

Page 30: Lecture 1 An Introduction to Computer, Programming and Java 1.

30

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

You need an interpreter to run Java programs. The programs are compiled into the Java Virtual Machine code called bytecode. The bytecode is machine-independent and can run on any machine that has a Java interpreter, which is part of the Java Virtual Machine (JVM).

Page 31: Lecture 1 An Introduction to Computer, Programming and Java 1.

31

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

Java compilers can detect many problems that would first show up at execution time in other languages.

Java has eliminated certain types of error-prone programming constructs found in other languages.

Java has a runtime exception-handling feature to provide programming support for robustness.

Page 32: Lecture 1 An Introduction to Computer, Programming and Java 1.

32

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

Java implements several security mechanisms to protect your system against harm caused by stray programs.

Page 33: Lecture 1 An Introduction to Computer, Programming and Java 1.

33

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

Write once, run anywhere

With a Java Virtual Machine (JVM), you can write one program that will run on any platform.

Page 34: Lecture 1 An Introduction to Computer, Programming and Java 1.

34

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

Because Java is architecture neutral, Java programs are portable. They can be run on any platform without being recompiled.

Page 35: Lecture 1 An Introduction to Computer, Programming and Java 1.

35

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

Because Java is interpreted, the bytecode is not directly executed by the system, but is run through the interpreter. However, its speed is more than adequate for most interactive applications, where the CPU is often idle, waiting for input or for data from other sources.

Page 36: Lecture 1 An Introduction to Computer, Programming and Java 1.

36

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

Multithread programming is smoothly integrated in Java, whereas in other languages you have to call procedures specific to the operating system to enable multithreading.

Page 37: Lecture 1 An Introduction to Computer, Programming and Java 1.

37

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

Java was designed to adapt to an evolving environment. New code can be loaded on the fly without recompilation. There is no need for developers to create, and for users to install, major new software versions. New features can be incorporated transparently as needed.

Page 38: Lecture 1 An Introduction to Computer, Programming and Java 1.

38

JDK Versions JDK 1.02 (1995) JDK 1.1 (1996) JDK 1.2 (1998) JDK 1.3 (2000) JDK 1.4 (2002) JDK 1.5 (2004) a. k. a. JDK 5 or Java 5 JDK 1.6 (2006) a. k. a. JDK 6 or Java 6 JDK 1.7 (2010) a. k. a. JDK 7 or Java 7

http://www.oracle.com/technetwork/java

Page 39: Lecture 1 An Introduction to Computer, Programming and Java 1.

39

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 and Java ServerPages.

Java Micro Edition (J2ME). J2ME can be used to develop applications for

mobile devices such as cell phones.

Page 40: Lecture 1 An Introduction to Computer, Programming and Java 1.

40

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

Listing 1.1

Page 41: Lecture 1 An Introduction to Computer, Programming and Java 1.

41

Creating and Editing Using NotePadTo use NotePad, type

notepad Welcome.java from the command prompt.

Page 42: Lecture 1 An Introduction to Computer, Programming and Java 1.

Creating and Editing Using WordPad

42

To use WordPad, type write Welcome.java

from the command prompt.

Page 43: Lecture 1 An Introduction to Computer, Programming and Java 1.

43

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)

Page 44: Lecture 1 An Introduction to Computer, Programming and Java 1.

Compiling and Running Java from the Command Window

44

Set path to JDK bin directory set path=c:\Program Files\java\jdk1.7.0\bin

Set classpath to include the current directory set classpath=.

Compile javac Welcome.java

Run java Welcome

Page 45: Lecture 1 An Introduction to Computer, Programming and Java 1.

45

Acknowledgement The lecture notes for this course include

slides from: Daniel Liang – Introduction to Java

Programming, 2011 Antonio Martinez – Java Programming: From

the Ground Up, 2010