Introduction to Java - DTIC

116 downloads 881 Views 227KB Size Report
Become comfortable with object oriented programming: Learn to think in objects. •. Learn the essentials of the Java class library, and learn how to learn about ...
1

2

Objectives

JAVA



No background assumptions



Learn the Java programming language: syntax and semantics



Become comfortable with object oriented programming: Learn to think in objects



Learn the essentials of the Java class library, and learn how to learn about other parts of the library when you need them.



Practicalities left to prácticas

3

4

References

• Deitel, Java How to Program, Third Edition (1999)

Introduction to Java

• Eckel, Thinking in Java, Third Edition (2002)

(Deitel ch.1, Eckel ch.1)

• Web Resources (reference manuals, etc…)

5

Plan 1.

Basic Concepts

2.

History of Java

3.

Java Class Libraries

4.

Basics of a Typical Java Environment

5.

Thinking About Objects

6.

Interfaces

7.

Services

8.

The hidden implementation

9.

Inheritance

10. polymorphism

6

Basic Concepts • Is there a program that can be written in one language for which there is really no equivalent program in one of the other languages? – e.g. Is there a program that can be written in Java that has no equivalent in Pascal?

• Sapir-Whorf Hypothesis: – the languages we speak directly influence the way in which we view the world.

1

7

8

Basic Concepts

Basic Concepts • • • • • •

Church’s thesis • Any computable function can be computed by a Turing machine.

Compiler Interpreter Machine language Assembly language High-level language Object-Oriented language

– Why do we need all these different languages?

9

Basic Concepts •

• Machine language

Compiler

– +130004774; +1400593419; +1200274027 – “Natural language” of computer component – Machine dependent

– Compiler is a translator – Translates a high-level language into an a variety of machine language.



10

Basic Concepts

• Assembly language – LOAD BASEPAY; ADD OVERPAY; STORE TOTAL

Interpreter

– English-like abbreviations represent computer operations – Translator programs convert to machine language

– Execute high-level language programs without compilation 1. Gets next instruction from memory 2. Decodes the instruction into primitive machine operations 3. Executes the operations

• High-level language – total = basePay + overTimePay – Allows for writing more “English-like” instructions – Compiler convert to machine language

• O-O language (O-O Paradigm)

11

History of Java • Java – Originally for intelligent consumer-electronic devices – Then used for creating Web pages with dynamic content – Now also used for: • Develop large-scale enterprise applications • Enhance WWW server functionality • Provide applications for consumer devices (cell phones, etc.)

12

Java Class Libraries • Java Programs consist of pieces called classes • Classes – Include methods that perform tasks • Return information after task completion

– Used to build Java programs

• You can program each class or make use of Java class libraries • Java class libraries – Known as Java APIs (Application Programming Interfaces)

2

13

14

Notes

Basics of a Typical Java Environment

Avoid reinventing the wheel. Use existing pieces

• Java System: environment, language, API • Java programs normally undergo five phases – Edit

Typical Java building blocks

• Programmer writes program (and stores program on disk)

Classes from class libraries Your own classes and methods Other people´s classes and methods

– Compile • Compiler creates bytecodes from program

– Load • Class loader stores bytecodes in memory

Class libraries can improve program performance

– Verify • Verifier ensures bytecodes do not violate security requirements

– Execute

Class Libraries can also improve portabilty

Phase 1

Editor

Disk

Program is created in an editor and stored on disk in a file ending with .java.

Phase 2

Compiler

Disk

Compiler creates bytecodes and stores them on disk in a file ending with .class.

Phase 3

Class Loader

Disk

Phase 4

Bytecode Verifier

• Interpreter translates bytecodes into machine language

15

16

Notes Write programs as simple as possible

Primary Memory

. .. .. .

Class loader reads .class files containing bytecodes from disk and puts those bytecodes in memory.

Simply writing programs in Java does not guarantee portability

Primary Memory Bytecode verifier confirms that all bytecodes are valid and do not violate Java’s security restrictions.

Iterpreter vs Compiler

. .. .. .

Phase 5

Interpreter

Primary Memory

. .. .. .

Interpreter reads bytecodes and translates them into a language that the computer can understand, possibly storing data values as the program executes.

Typical Java environment.

17

18

Thinking About Objects

An object has an interface • •

How do you get an object to do useful work for you? The requests you can make of an object are defined by its interface

• Objects – Software components that model real-world items – Look all around you • People, animals, plants, cars, etc.

Light lt = new Light();

– Attributes

lt.on();

• Size, shape, color, weight, etc.

– Behaviors • Babies cry, crawl, sleep, etc.

– Objects that are identical except for their state during a program’s execution are grouped together into classes



there must be code somewhere to satisfy that request.

• • • •

This, along with the hidden data, comprises the implementation name of the type/class is Light, the name of this particular Light object is lt the requests that you can make of a Light object are to turn it on, etc. Light object created by defining a reference (lt) for that object and calling new to request a new object of that class.

3

19

20

An object provides services

The hidden implementation

• Think of an object as a service provider

• Class creators and client programmers • Class creator exposes only what’s necessary to the client programmer and keeps everything else hidden • Creator can change the hidden portion at will without worrying about the impact on anyone else. • The hidden portion could easily be corrupted by a careless or uninformed client • Hiding the implementation reduces program bugs • In Java

• “I could magically pull them out of a hat, what objects would solve my problem right away?” • High cohesion means that the various aspects of a software component (such as an object) “fit together” well. • One problem people have when designing objects is cramming too much functionality into one object.

– public – private – protected

21

Inheritance: reusing the interface

22

Inheritance: reusing the interface

• It seems a pity to create a class and then be forced to create a brand new one that might have similar functionality

in Java, keyword for inheritance is extends

• take the existing class, clone it, and then make additions and modifications to the clone → inheritance

• • •

base class or superclass or parent class derived class or inherited class or subclass or child class if base class is changed, derived class also changed

23

24

Interchangeable objects with polymorphism

Polymorphism A method in Java void doStuff(Shape s) { s.erase(); // ... s.draw(); } This method speaks to any Shape, so it is independent of the specific type of object

• Treat object not as the specific type, but instead as the base type • Allows to write code that doesn’t depend on specific types • In the shape example, methods manipulate generic shapes without respect to whether they’re circles, squares, etc.

Circle c = new Circle(); Triangle t = new Triangle(); Line l = new Line(); doStuff(c); doStuff(t); doStuff(l); calls to doStuff( ) automatically work correctly, regardless of the exact type of the object. doStuff(c); → Circle is being passed into a method that’s expecting a Shape.

4

25

Polymorphism

s.erase() // ... s.draw(); Notice that it doesn’t say “If you’re a Circle, do this, if you’re a Square, do that, etc.” Often, you don’t want to create an object of the base class, only to upcast to it (abstract keyword).

5