Introduction & The Java Virtual Machine Small Java Chapter 1

34
2005 Pearson Education, Inc. All rights rese 1 Introduction & The Java Virtual Machine Small Java Chapter 1 1

description

Introduction & The Java Virtual Machine Small Java Chapter 1. 1. Machine Languages, Assembly Languages and High-Level Languages. Machine language 1’s and 0’s Machine dependent Assembly language English-like abbreviations represent computer operations - PowerPoint PPT Presentation

Transcript of Introduction & The Java Virtual Machine Small Java Chapter 1

Page 1: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

1

Introduction & The Java Virtual Machine

Small Java Chapter 1

1

Page 2: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

Machine Languages, Assembly Languages and High-Level Languages

• Machine language– 1’s and 0’s– Machine dependent

• Assembly language– English-like abbreviations represent computer operations– Translator programs convert to machine language– ADD 12,14

• High-level language– Allows for writing more “English-like” instructions– Contains commonly used mathematical operations– Compiler converts to machine language– If X < 12 THEN …

• Interpreter– Execute high-level language programs without compilation

2

Page 3: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

FORTRAN, COBOL, Pascal and Ada

• FORTRAN– FORmula TRANslator

• COBOL– COmmon Business Oriented Language

• Pascal– Structured programming

• Ada– Multitasking

3

Page 4: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

BASIC, Visual Basic, Visual C++, C# and .NET

• BASIC– Beginner’s All-Purpose Symbolic Instruction Code

• .NET– .NET platform

• Visual Basic .NET– Based on BASIC

• Visual C++– Based on C++

• C#– Based on C++ and Java

4

Page 5: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

History of Java

– Originally for intelligent consumer-electronic devices

– Then used for creating Web pages with dynamic content

– Now also used to:• Develop large-scale enterprise applications• Provide applications for consumer devices (cell

phones, etc.)

5

Page 6: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

Java Is Not JavaScript!

• Java is a programming language. You write programs in it and compile them.

• Javascript is imbedded in a web page and run by Internet Explorer or Firefox

• Java applets are programs that run in a web browser

• We will only get to Java applications

6

Page 7: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

With C++ and most compiled languages

7

Compiler For Windows PC

Your Program

If x = 5 then…

1011 0011

PC with Windows

Compiler For Mac

0011 1111

Mac

Source Code

Page 8: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

Java Virtual Machine

8

Java Compiler

Your Program

If x = 5 then…

1011 0011

PC with Windows

ByteCode

Windows JVM Mac JVM

0011 1111

Mac

Source Code

Page 9: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

Java Virtual Machine

• Why and how is this different?

• Write once, run anywhere– Bytecode is a ‘universal language’

• Why isn’t all software written like this?– Xbox 360 games would work on PS 3…

9

Page 10: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

Software Engineering Observation

Use a building-block approach to create programs.

Avoid reinventing the wheel—use existing pieces wherever possible.

Called software reuse, this practice is central to object-oriented programming.

10

Page 11: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

Java Class Libraries

• Classes– Include methods (functions) that perform tasks

• Return information after task completion– Used to build Java programs

• Java provides class libraries (of functions or methods)– Known as Java APIs (Application Programming Interfaces)– The stuff built-in to Java– Don’t re-invent the wheel!

11

Page 12: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

Software Engineering Observation When programming in Java, you will typically use the following building blocks:

Classes and methods from class libraries,

Classes and methods you create yourself

Classes and methods that others create and make available to you.

12

Page 13: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

Performance Tip Using Java API classes and methods instead of writing your own versions can improve program performance, because they are carefully written to perform efficiently.

This technique also shortens program development time.

13

Page 14: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

Typical Java Development Environment

• Java programs normally undergo five phases– Edit

• Programmer writes program (and stores program on disk)

– Compile• Compiler creates bytecodes from program

– Load• Class loader stores bytecodes in memory

– Verify• Bytecode Verifier confirms bytecodes do not violate security restrictions

– Execute• JVM translates bytecodes into machine language

14

Page 15: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

15

Fig. 1.1 | Typical Java development environment.

Page 16: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

Common Programming Error

Errors like division by zero occur as a program runs, so they are called runtime errors or execution-time errors.

Fatal runtime errors cause programs to terminate immediately without having successfully performed their jobs.

Nonfatal runtime errors allow programs to run to completion, often producing incorrect results.

16

Page 17: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

Good Programming Practice Your computer and compiler are good teachers.

Study each error or warning message you get when you compile your programs (called compile-time errors or compilation errors), and correct the programs to eliminate these messages.

17

Page 18: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

Portability Tip

It is easier to write portable programs in Java than in other programming languages

Portable to other platforms (from X-box to PS3)

However, differences between compilers, JVMs and computers can make portability difficult to achieve.

Simply writing programs in Java does not guarantee portability.

18

Page 19: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

Error-Prevention Tip

Always test your Java programs on all systems on which you intend to run them, to ensure that they will work correctly for their intended audiences.

19

Page 20: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

Software Engineering Observation

The Java Development Kit comes with source code for the Java API classes to determine how the classes work and to learn additional programming techniques.

20

Page 21: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

21

Fig. 1.2 | Opening a Windows XP Command Prompt and changing directories.

Using the cd command to

change directories File location of the ATM application

Test-Driving a Java ApplicationUsing Command Line

Page 22: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

22

Fig. 1.3 | Using the java command to execute the ATM application.

Page 23: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

23

Fig. 1.4 | Prompting the user for an account number.

ATM welcome message Enter account number prompt

Page 24: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

24

Fig. 1.5 | Entering a valid PIN number and displaying the ATM application's main menu.

Enter valid PIN ATM main menu

Page 25: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

25

Fig. 1.6 | ATM application displaying user account balance information.

Account balance information

Page 26: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

26

Fig. 1.7 | Withdrawing money from the account and returning to the main menu.

ATM withdrawal menu

Page 27: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

27

Fig. 1.8 | Checking new balance.

Confirming updated account balance information after withdrawal transaction

Page 28: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

28

Fig. 1.9 | Ending an ATM transaction session.

ATM goodbye message

Account number prompt for next user

Page 29: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

Programming Paradigms

• Traditional Programming vs. Object Oriented Programming

Software objects attempt to model the real world

Objects have data (attributes) and behaviors (methods or functions)

29

Page 30: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

Software Engineering Case Study: Introduction to Object Technology and the UML

• Object orientation

• Unified Modeling Language (UML)– Graphical language that uses common notation– Allows developers to represent object-oriented

designs

30

Page 31: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

Software Engineering Case Study (Cont.)

• Objects– Reusable software components that model real-world

items– People, animals, plants, cars, etc.– Attributes

• Size, shape, color, weight, etc.– Behaviors

• Babies cry, crawl, sleep, etc.

31

Page 32: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

Software Engineering Case Study

• Object-oriented design (OOD)– Models real-world objects– Models communication among objects– Encapsulates attributes and operations (behaviors)

• Information hiding• Communication through well-defined interfaces

• Object-oriented language– Programming in object-oriented languages is called

object-oriented programming (OOP)– Java

32

Page 33: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

Software Engineering Case Study

• Object-Oriented Analysis and Design (OOA/D)

– Essential for large programs– Analyze program requirements, then develop

solution– UML

• Unified Modeling Language

33

Page 34: Introduction & The Java Virtual Machine Small Java Chapter 1

2005 Pearson Education, Inc. All rights reserved.

Software Engineering Case Study• UML

– Graphical representation scheme

– Enables developers to model object-oriented systems

– Flexible and extensible

34

Player

health

armor

Run (direction)

Fire (coordinates)

jump