chapter 1 introduction to java - cis.csuohio.educis.csuohio.edu/~sschung/esc120/01slide.pdf · 1...

68
1 Chapter 1 Introduction to Computers, Programs, and Java

Upload: others

Post on 01-Oct-2020

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

1

Chapter 1

Introduction to Computers,

Programs, and Java

Page 2: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

2

Objectives To review computer basics, programs, and operating systems (§§1.2-1.4).

To explore the relationship between Java and the World Wide Web (§1.5).

To distinguish the terms API, IDE, and JDK (§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).

To create, compile, and run Java programs (§1.8).

(GUI) To display output using the JOptionPane output dialog boxes (§1.9).

Page 3: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

3

What is a Computer?

A typical computer consists of:

a CPU, memory, hard disk,

monitor, printer, IO ports and

communication devices.

CPU

e.g., Disk, CD,

and Tape

Input

Devices

e.g., Keyboard,

Mouse

e.g., Monitor,

Printer

Communication

Devices

e.g., Modem,

and NIC

Storage

Devices

Memory Output

Devices

Bus

Page 4: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

4

What is a Computer? Chassis XPS 8500, White Chassis w/19:1 media card reader OPERATING SYSTEM Genuine Windows® 7 Home Premium, 64Bit, English PROCESSORS 3rd Generation Intel® Core™ i7-3770 processor 3.40 GHz with Turbo Boost 2.0 up to 3.90 GHz MEMORY 12GB Dual Channel DDR3 1600MHz - 4 DIMMs HARD DRIVE 2TB 7200RPM, SATA Hard Drive 6.0 Gb/s + 32GB SSD SRT Enhancement VIDEO CARD AMD Radeon™ HD 7770 2GB GDDR5 OPTICAL DRIVE 16X CD/DVD burner (DVD+/-RW), write to CD/DVD WIRELESS Dell Wireless 1703 802.11b/g/n, Bluetooth v4.0+LE SOUND CARD Integrated 7.1 with WAVE MAXXAudio 4 USB 3.0 Integrated 10/100/1000 Ethernet Dell Datasafe DataSafe 2.0 Online Backup 2GB for 1 year Wireless Driver Dell SRV Software 1703 Additional Software Additional Software Power Cord US Power Cord May 12, 2012 Source: www.dell.com

Page 5: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

5

CPU • The central processing unit (CPU) is the brain of a computer.

• It retrieves instructions from memory and executes them.

• The CPU speed is measured in Gigahertz (GHz), with 1 gigahertz equaling 1

billion pulses per second.

• For example: Intel i7 core frequency is 3.45GHz (Up To 3.73GHz w/ Turbo)

and includes six core CPUs in a package (cell phone CPU’s @ 1.5Ghz)

CPU

e.g., Disk, CD,

and Tape

Input

Devices

e.g., Keyboard,

Mouse, Stylus

e.g., Monitor,

Printer

Communication

Devices

e.g., Modem,

and NIC

Storage

Devices

Memory Output

Devices

Bus

Page 6: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

6

Memory

• Memory is used to store OS, and user data and program instructions for

CPU to execute.

• A memory unit is an ordered sequence of bytes, each holds eight bits.

• A program and its data must be brought to memory before they can be

executed.

• Typical laptops today support between 4-16 Gigabytes

CPU

e.g., Disk, CD,

and Tape

Input

Devices

e.g., Keyboard,

Mouse

e.g., Monitor,

Printer

Communication

Devices

e.g., Modem,

and NIC

Storage

Devices

Memory Output

Devices

Bus

Page 7: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

7

Memory

Byte B 1B (8 bits)

Kilobyte KB 1000 B

Megabyte MB 1000 KB

Gigabyte GB 1000 MB

Terabyte TB 1000 GB

Petabyte PB 1000 TB

Exabyte EB 1000 PB

CPU

e.g., Disk, CD,

and Tape

Input

Devices

e.g., Keyboard,

Mouse

e.g., Monitor,

Printer

Communication

Devices

e.g., Modem,

and NIC

Storage

Devices

Memory Output

Devices

Bus

The contents of a DVD 4.7 gigabytes

Printed collections of Library of Congress 10 terabytes

All printed material in the world 200 petabytes

All words ever spoken by human beings 5 exabytes

Source (May 19, 2014):

http://www2.sims.berkeley.edu/research/projects/how-much-

info/datapowers.html

Page 8: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

.

.

.

2000

2001

2002

2003

2004

.

.

.

01001010

01100001

01110110

01100001

00000011

Memory

content

Memory

address

Encoding for character ‘J’ Encoding for character ‘a’ Encoding for character ‘v’ Encoding for character ‘a’ Encoding for number 3

8

How Data is Stored? Numbers, characters, and strings, are encoded as a series of bits (zeros and ones).

The encoding scheme varies (ASCII, Unicode). For example, character ‘J’ is represented by 01001010 in one byte (ASCII Hex: 4A).

A small number such as 3 can be stored in a single byte.

If computer needs to store a large number that cannot fit into a single byte, it uses a number of adjacent bytes.

No two data can share or split a same byte.

A byte is the minimum storage unit.

Page 9: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

9

Storage Devices

Memory is volatile, information is lost when the power is off.

Programs and data are permanently stored on storage devices and are

moved to memory when the computer actually uses them.

Currently there are three main types of storage devices:

Disk drives: Mechanical/SSD disks (and floppy disks),

Optical drives: CD/DVD drives, and

Magnetic drives: Tape drives.

CPU

e.g., Disk, CD,

and Tape

Input

Devices

e.g., Keyboard,

Mouse

e.g., Monitor,

Printer

Communication

Devices

e.g., Modem,

and NIC

Storage

Devices

Memory Output

Devices

Bus

Page 10: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

10

Storage Devices

CPU

e.g., Disk, CD,

and Tape

Input

Devices

e.g., Keyboard,

Mouse

e.g., Monitor,

Printer

Communication

Devices

e.g., Modem,

and NIC

Storage

Devices

Memory Output

Devices

Bus

Dimensions

Capacity

Rotational speed / Buffer size

Interface / Transfer rate

Page 11: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

11

Output Devices: Monitor

The monitor displays information

(text and graphics).

The resolution and dot pitch

determine the quality of the display.

CPU

e.g., Disk, CD,

and Tape

Input

Devices

e.g., Keyboard,

Mouse

e.g., Monitor,

Printer

Communication

Devices

e.g., Modem,

and NIC

Storage

Devices

Memory Output

Devices

Bus

Page 12: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

12

Monitor Resolution and Dot Pitch The resolution specifies the number of pixels per square inch.

Pixels (short for “picture elements”) are tiny dots that form an image

on the screen. The higher the resolution, the sharper and clearer the

image is.

PC monitors are usually 15-inch, 17-inch, 19-inch, or 21-inch.

For a 15-inch monitor, a comfortable resolution setting would be

640480 (307,200 pixels).

resolution

The dot pitch is the amount of space between pixels.

The smaller the dot pitch, the better the display.

dot pitch

Page 13: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

13

Communication Devices A regular modem uses a phone line and can transfer data in a speed up to

56 Kbps (bits per second).

DSL (digital subscriber line) and cable modem can transfer data in the

range 128Kbps – 3Gbps (upload/download)

Network interface card (NIC) is a device to connect a computer to a local

area network (LAN).

A typical type of NIC, called 10BaseT, can transfer data at 10 mbps

Check your Internet speed: http://speedtest.connectedoh.org/

CPU

e.g., Disk, CD,

and Tape

Input

Devices

e.g., Keyboard,

Mouse

e.g., Monitor,

Printer

Communication

Devices

e.g., Modem,

and NIC

Storage

Devices

Memory Output

Devices

Bus

Page 14: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

14

Programs

Computer programs, known as software, are instructions to the

computer.

You tell a computer what to do through programs.

Computers do not understand human languages, so you need to

use computer languages to communicate with them.

Programs are written using programming languages.

Page 15: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

15

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.

• Write program with native machine language is a tedious process, 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 16: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

16

Programming Languages Machine Language Assembly Language High-Level Language

Assembly languages were developed to make programming easier. 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 17: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

17

Programming Languages Machine Language Assembly Language High-Level Language

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

and program.

For example, the following is a high-level language statement that

computes a student’s final grade

final grade = 0.20 * homeworkGrade + 0.30 * midterm + 0.5 * finalExam;

Page 18: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

18

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)

• Objective C (used to develop Apple iOS apps)

• 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)

• Perl, PHP (scripting language used for web-development)

• Python,

JavaScript,…

• Java (We use it in the book)

Page 19: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

19

Compiling Source Code

1. A program written in a high-level language is called a source

program.

2. A program called a compiler is used to translate the source

program into a machine language program called an object

program.

3. 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 20: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

20

Operating Systems

1. The operating system (OS) is a

program that manages and

controls a computer’s

activities.

2. You are probably using OSX,

Linux, Windows 7, 8, XP, or

Vista.

3. Windows is currently the most

popular PC operating system.

4. Application programs such as

an Internet browser and a word

processor cannot run without

an operating system.

User

Application Programs

Operating System

Hardware

Page 21: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

21

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, and appliances.

Java is a general purpose programming language.

Java is the primary Internet programming language.

Page 22: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

22

Java, Web, and Beyond

• Java can be used to develop:

• mobile applications,

• games,

• Web-based content, and

• enterprise software

Page 23: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

23

Examples of Java’s Versatility (Applets)

Page 24: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

24

PDA, Tablets, Appliances, Cell Phones …

Page 25: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

25

Java’s History

• Designed by James Gosling @ Sun Microsystems

originally intended for interactive TV environment.

• Released on May 20, 1995, soon followed by Java enabled web-browser such as: HotJava, Netscape, IE.

• On 2007, Sun makes all of Java's core code available under free software/open-source distribution terms

• On 2010 Oracle acquires Sun.

• Oracle vs. Google-Android (2012) sets legal precedent: APIs cannot be copyrighted (Oracle lost the case).

• According to Oracle, over 4 billion devices worldwide operate using Java (2013)

Duke,

original

Java mascot

Page 26: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

26

Characteristics of Java 1. Java is Simple

2. Java is Object-Oriented

3. Java is Distributed

4. Java is Interpreted

5. Java is Robust

6. Java is Secure

7. Java is Architecture-Neutral

8. Java is Portable

9. Java's Performance

10. Java is Multithreaded

11. Java is Dynamic

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

Page 27: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

27

Characteristics of Java 1. Java is Simple

2. Java is Object-Oriented

3. Java is Distributed

4. Java is Interpreted

5. Java is Robust

6. Java is Secure

7. Java is Architecture-

Neutral

8. Java is Portable

9. Java's Performance

10. Java is Multithreaded

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

Companion Website

Page 28: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

28

Characteristics of Java 1. Java is Simple

2. Java is Object-Oriented

3. Java is Distributed

4. Java is Interpreted

5. Java is Robust

6. Java is Secure

7. Java is Architecture-

Neutral

8. Java is Portable

9. Java's Performance

10. Java is Multithreaded

11. Java is Dynamic

Java is inherently 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.

Companion Website

Page 29: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

29

Characteristics of Java 1. Java is Simple

2. Java is Object-Oriented

3. Java is Distributed

4. Java is Interpreted

5. Java is Robust

6. Java is Secure

7. Java is Architecture-

Neutral

8. Java is Portable

9. Java's Performance

10. Java is Multithreaded

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

Companion Website

Page 30: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

30

Characteristics of Java 1. Java is Simple

2. Java is Object-Oriented

3. Java is Distributed

4. Java is Interpreted

5. Java is Robust

6. Java is Secure

7. Java is Architecture-

Neutral

8. Java is Portable

9. Java's Performance

10. Java is Multithreaded

11. 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).

Companion Website

Page 31: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

31

Characteristics of Java 1. Java is Simple

2. Java is Object-Oriented

3. Java is Distributed

4. Java is Interpreted

5. Java is Robust

6. Java is Secure

7. Java is Architecture-

Neutral

8. Java is Portable

9. Java's Performance

10. Java is Multithreaded

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

Companion Website

Page 32: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

32

Characteristics of Java 1. Java is Simple

2. Java is Object-Oriented

3. Java is Distributed

4. Java is Interpreted

5. Java is Robust

6. Java is Secure

7. Java is Architecture-

Neutral

8. Java is Portable

9. Java's Performance

10. Java is Multithreaded

11. Java is Dynamic

Java implements several security

mechanisms to protect your system

against harm caused by stray

programs.

Companion Website

Page 33: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

33

Characteristics of Java 1. Java is Simple

2. Java is Object-Oriented

3. Java is Distributed

4. Java is Interpreted

5. Java is Robust

6. Java is Secure

7. Java is Architecture-

Neutral

8. Java is Portable

9. Java's Performance

10. Java is Multithreaded

11. Java is Dynamic

Write once, run anywhere

With a Java Virtual Machine (JVM), you

can write one program that will run on

any platform.

Companion Website

Page 34: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

34

Characteristics of Java 1. Java is Simple

2. Java is Object-Oriented

3. Java is Distributed

4. Java is Interpreted

5. Java is Robust

6. Java is Secure

7. Java is Architecture-

Neutral

8. Java is Portable

9. Java's Performance

10. Java is Multithreaded

11. Java is Dynamic

Because Java is architecture neutral,

Java programs are portable. They can

be run on any platform without being

recompiled.

Companion Website

Page 35: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

35

Characteristics of Java 1. Java is Simple

2. Java is Object-Oriented

3. Java is Distributed

4. Java is Interpreted

5. Java is Robust

6. Java is Secure

7. Java is Architecture-

Neutral

8. Java is Portable

9. Java's Performance

10. Java is Multithreaded

11. Java is Dynamic

Although slower than executable code

the speed of interpreted Java bytecode

is reasonably good. Use Just-In-Time

compiler feature for critical apps.

Companion Website

Page 36: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

36

Characteristics of Java 1. Java is Simple

2. Java is Object-Oriented

3. Java is Distributed

4. Java is Interpreted

5. Java is Robust

6. Java is Secure

7. Java is Architecture-

Neutral

8. Java is Portable

9. Java's Performance

10. Java is Multithreaded

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

Companion Website

Page 37: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

37

Characteristics of Java 1. Java is Simple

2. Java is Object-Oriented

3. Java is Distributed

4. Java is Interpreted

5. Java is Robust

6. Java is Secure

7. Java is Architecture-

Neutral

8. Java is Portable

9. Java's Performance

10. Java is Multithreaded

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

Companion Website

Page 38: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

38

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.

This book uses J2SE to introduce Java programming.

http://www.oracle.com

Page 39: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

39

Popular Java IDEs

• NetBeans Open Source by Sun/Oracle

• www.netbeans.org

• Eclipse Open Source by IBM

• www.eclipse.org

Page 40: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

40

Java Programming Preliminaries

Windows Users:

After installing the JDK ( say in C:\Program Files\Java\jdk1.7.0_03)

You need to create a system-wide reference to the Java directory.

Steps

1. Start button > Right click on Computer > Properties > Advanced

System Setting >(Advanced Tab) Environment Variables.

2. On “System Variables” panel, click “New” > on “New System

Variable” window enter Variable name: “Java_Home”, and Variable

value: “C:\Program Files\Java\jdk1.7.0_03” > click OK

3. Again, on “System Variables” panel, look for “Path” variable, click

on Edit > prefix the path string with the following reference:

%JAVA_HOME%\bin; (caution: include the semicolon)

Page 41: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

41

Java Programming Preliminaries

Windows Users: (continuation)

Steps

4. [Important for the use of Packages – Explained later]

Once more, on “System Variables” panel, look for “classPath”

variable. Click “New…” (in case it doesn’t exist) or Edit. Enter the

following string .;c:/; (caution: include the semicolons)

(later on you may execute a class by typing a command such as:

java csu.matos.MyClass1 where csu.matos is the package

name, and the class is located in the directory c:\csu\matos )

Page 42: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

42

Java

Programming

Preliminaries 1/2

Page 43: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

43

Java

Programming

Preliminaries 2/2

Page 44: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

44 44

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 45: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

45

Creating and Editing Using NotePad

To use NotePad, type notepad Welcome.java

from the DOS prompt.

Page 46: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

46

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 47: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

Java Bytecode

Java Virtual Machine

Any Computer

47

Compiling Java Source Code

1. With Java, you write the program once, and compile the source program into a special type of object code, known as bytecode.

2. The bytecode can then run on any computer with a Java Virtual Machine (JVM), as shown below.

3. Java Virtual Machine is a software that interprets Java bytecode.

Page 48: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

48

//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

animation

Page 49: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

49

//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

animation

Page 50: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

50

//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 animation

print a message to the

console

Page 51: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

51

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

Companion Website

Page 52: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

52

Anatomy of a Java Program

1. Comments

2. Reserved words

3. Modifiers

4. Statements

5. Blocks

6. Classes

7. Methods

8. The main method

Page 53: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

53

Comments

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.

Three types of comments in Java.

Page 54: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

54

Reserved Words

1. Reserved words or keywords are words that have a specific

meaning to the compiler and cannot be used for other purposes in

the program.

2. For example, when the compiler sees the word class, it understands

that the word after class is the name for the class.

3. Other reserved words in Listing 1.1 are public, static, String and

void. Their use will be introduced later in the book.

Page 55: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

55

Reserved Words

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

Page 56: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

56

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.

Page 57: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

57

Statements

A statement represents an action or a sequence of actions.

The statement

System.out.println(“ Hello world ") ;

in the previous program is a statement to display the greeting

"Welcome to Java!"

Every statement in Java ends with a semicolon ( ; ).

Page 58: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

58

Blocks

A pair of braces {…} in a program forms a block that

groups related components of a program.

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

Meth

od

blo

ck

Class b

lock

Page 59: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

59

Classes

1. The class is the essential Java construct.

2. A class is a template or blueprint for objects.

3. To program in Java, you must understand classes and be able to

write and use them.

4. Programs are defined by using one or more classes.

public class Welcome { ... Class contents }

Page 60: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

60

Methods 1. 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.

2. It can be used even without fully understanding the details of how it

works.

3. It is used by invoking a statement with a string argument. The string

argument is enclosed within parentheses.

4. In this case, the argument is " Hello world " You can call the

same println method with a different argument to print a different

message.

Page 61: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

61

main Method The Java interpreter executes the application by invoking the main

method.

The main method looks like this:

public static void main(String[] args) {

// statements;

}

Page 62: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

62

Displaying Text in a Message Dialog Box

A more colorful version of the test program follows

import javax.swing.JOptionPane;

public class Welcome {

public static void main(String[] args) {

JOptionPane.showMessageDialog (

null,

"Welcome to Java!",

"Display Message",

JOptionPane.INFORMATION_MESSAGE);

}

}

Page 63: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

63

Two Ways to Invoke the Method One way is to use a statement as shown in the example:

JOptionPane.showMessageDialog(null,

x,

y,

JOptionPane.INFORMATION_MESSAGE);

where x is a string for the text to be displayed, and y is a string for

the title of the message dialog box.

The other is to use a statement like this:

JOptionPane.showMessageDialog(null, x);

where x is a string for the text to be displayed.

Page 64: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

64

Page 65: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

65

How much information? http://www2.sims.berkeley.edu/research/projects/how-much-info/datapowers.html

Many of the following facts were taken from Roy Williams "Data Powers of Ten" page at Caltech. Byte [ 8 bits]

0.1 bytes: a binary decision; 1 byte: a single character; 10 bytes: a single word; 100 bytes: a telegram ORr a punched card;

Kilobyte [ 1,000 bytes OR 103 bytes] 1 Kilobyte: A very short story; 2 Kilobytes: A typewritten page; 10 Kilobytes: An encyclopaedic page OR a deck of punched cards; 10 Kilobytes: static web page; 50 Kilobytes: A compressed document image page; 100 Kilobytes: A low-resolution photograph; 200 Kilobytes: A box of punched cards; 500 Kilobytes: A very heavy box of punched cards;

Megabyte [ 1,000,000 bytes OR 106 bytes] 1 Megabyte: A small novel OR a 3.5 inch floppy disk; 2 Megabytes: A high resolution photograph; 5 Megabytes: The complete works of Shakespeare OR 30 seconds of TV-quality video; 10 Megabytes: A minute of high-fidelity sound OR a digital chest X-ray; 20 Megabytes: A box of floppy disks; 50 Megabytes: A digital mammogram; 100 Megabytes: 1 meter of shelved books OR a two-volume encyclopaedic book; 200 Megabytes: A reel of 9-track tape OR an IBM 3480 cartridge tape; 500 Megabytes: A CD-ROM OR the hard disk of a PC;

Page 66: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

66

How much information? http://www2.sims.berkeley.edu/research/projects/how-much-info/datapowers.html

Gigabyte [ 1,000,000,000 bytes OR 109 bytes] 1 Gigabyte: a pickup truck filled with paper OR a symphony in high-fidelity sound OR a movie at TV quality; 2 Gigabytes: 20 meters of shelved books OR a stack of 9-track tapes; 5 Gigabytes: 8mm Exabyte tape; 20 Gigabytes: A good collection of the works of Beethoven OR 5 Exabyte tapes OR a VHS tape used for digital data; 50 Gigabytes: A floor of books OR hundreds of 9-track tapes; 100 Gigabytes: A floor of academic journals OR a large ID-1 digital tape; 200 Gigabytes: 50 Exabyte tapes; 500 Gigabytes: The biggest FTP site.

Terabyte [ 1,000,000,000,000 bytes OR 1012 bytes] 1 Terabyte: An automated tape robot OR all the X-ray films in a large technological hospital OR 50000 trees made into paper and printed OR daily rate of EOS data (1998); 2 Terabytes: An academic research library OR a cabinet full of Exabyte tapes; 10 Terabytes: The printed collection of the US Library of Congress; 50 Terabytes: The contents of a large Mass Storage System; 400 Terabytes: National Climactic Data Center (NOAA) database;

Petabyte [ 1,000,000,000,000,000 bytes OR 1015 bytes] 1 Petabyte: 3 years of EOS data (2001); 2 Petabytes: All US academic research libraries; 8 Petabytes: All information available on the Web; 20 Petabytes: Production of hard-disk drives in 1995; 200 Petabytes: All printed material OR production of digital magnetic tape in 1995;

Exabyte [ 1,000,000,000,000,000,000 bytes OR 1018 bytes] 2 Exabytes: Total volume of information generated worldwide annually. 5 Exabytes: All words ever spoken by human beings.

Zettabyte [ 1,000,000,000,000,000,000,000 bytes OR 1021 bytes] Yottabyte [ 1,000,000,000,000,000,000,000,000 bytes OR 1024 bytes]

Page 67: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

67 67

Appendix. The Size of the Mobile Market – 2009 Extracted from: http://gizmodo.com/5489036/cellphone-overshare

67

3.05 bn

SMS users worldwide

2.6 SMS per

day per person world average . The most used

written communication

tool of the planet

600 m IM users

worldwide

1.3 bn

Email users worldwide

2009 How SMS compares as a text communication application

Page 68: Chapter 1 Introduction to Java - cis.csuohio.educis.csuohio.edu/~sschung/ESC120/01slide.pdf · 1 Chapter 1 Introduction to Computers, ... 1. Java is Simple 2. Java is Object-Oriented

68

$600 bn Voice

$130 bn Messaging

$70 bn Non-messaging

2009 Mobile Revenue

The Size of the Mobile Market – 2009

Revenue Year 2009

Microsoft

$78bn

Toyota Motors

$204bn

Exxon Mobil

$301bn

Extracted from:

http://gizmodo.com/5489036/cellphone-overshare

http://www.microsoft.com/investor/reports/ar09/10k_fr_bal.html

Exxon Mobil 2009 Summary Annual Report

2010 Toyota Annual Report (pp 12)

Revenue is the

collective amount of

income made by a

company (usually from

the sales of goods &

services)