Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 8 February 29, 2000. Topics Questions about Exercise 4, due Thursday? Object Based Programming (Chapter 8) –Basic Principles –Methods –Fields.

Similar presentations


Presentation on theme: "Lecture 8 February 29, 2000. Topics Questions about Exercise 4, due Thursday? Object Based Programming (Chapter 8) –Basic Principles –Methods –Fields."— Presentation transcript:

1 Lecture 8 February 29, 2000

2 Topics Questions about Exercise 4, due Thursday? Object Based Programming (Chapter 8) –Basic Principles –Methods –Fields –Memory Structures

3 Understanding Objects: Basic Principles Need to know the difference between classes and objects. Need to know how classes are represented on disk and how classes and objects are represented in the computer’s memory. –Need to know about memory and disk. Need to know how to define classes and how to instantiate them to produce objects. Need to know ways to define, declare, and reference fields (variables) and methods.

4 Computers and Virtual Machines Memory (RAM) Processor (CPU) Instructions and DataResults Disk Input Output LoadStore

5 Processor/Memory/Disk Processor interprets instructions, which are encoded as binary numbers. –Fetch an instruction from memory. –Interpret (execute) it. –Repeat previous two steps forever. Memory (RAM) consists of bytes, each of which has an associated address. Disk is non-volatile (retains information when turned off), and less expensive than RAM. But slower to access.

6 Memory Holds the instructions to be interpreted. –Instructions correspond to method definitions. –Never need more than one copy of a method in memory because the instructions never change. Holds the operands (data) that the instructions operate on. –Operands correspond to fields (variables). –Variable names correspond to memory addresses. Addresses aren’t really there. (Like “third house from the corner.”)

7 Some Synonyms Input – Copy information from disk to memory. –Read - when getting part of a file, like a line of text. –Load - when getting a program to be executed. Output – Copy information from memory to disk. –Write - when outputting a piece of information. –Print - when the destination is a terminal window or a printer rather than a disk file. Load – Copy information from memory to CPU. –Read – generic substitute for “load.” –Fetch – when getting an instruction to execute. Store – Copy information from CPU to memory. –Write – generic substitute for “store.”

8 Java Virtual Machine (JVM) Software that interprets JVM instructions. –Could be hardware, like a real CPU, but isn’t. –There are JVMs for lots of different real CPUs, not just Intel’s or Sun’s. –Allows the same binary instructions to run on different real CPUs. Uses real I/O devices, not simulated ones. Examples –java.exe –appletviewer.exe –Web browsers

9 Classes When you write a Java “program” you define one or more classes. –The java compiler generates one disk file for each class, with a filename consisting of the class name followed by “.class” In order to do anything with a class, its class file first has to be loaded into memory. –Transfer contents of the.class file into RAM. –Link the class with other classes already in the Java Virtual Machine’s RAM.

10 Objects Once a class has been loaded, you can create objects that are instances of the class. –Classes may be instantiated zero, one, or multiple times, depending on the needs of a particular program. –Instantiation is always done using the new operator. Allocate memory for the object’s fields. Initialize the fields. Invoke a constructor to complete initialization. –Compiler provides a default constructor that takes no arguments if the class doesn’t have any constructors defined explicitly.

11 Static and Instance Fields Defined inside a class, but outside any method. –Static fields have the static modifier. –Instance fields do not have the static modifier. An instance field is created when a new object is created. –The memory for an instance field is inside the memory allocated for the object. There can be zero, one, or multiple copies of an instance field in memory, depending on how many times a class has been instantiated. A static field is created when a class is loaded. –The memory for a static field is inside the memory for the class. There is never more than one copy of a static field in memory because a particular class is loaded into memory just once.

12 Static and Instance Methods All methods are defined inside classes. –Static methods have the static modifier. –Instance methods do not have the static modifier. An instance method is one that is associated with a particular object. –It refers to its own instance’s fields by name. –Or by using a special reference variable named this. A static method is not associated with an object. –Sometimes called a “class method.” Both kinds are loaded into memory exactly one time, when the class file is loaded. –Instance methods receive this as an extra hidden parameter when invoked (called).

13 An Object in Memory Reference Variable Class Pointer Instance Field Static Field This part is created when the class is instantiated This part is created when the class file is loaded into memory method

14 Multiple Objects of One Class

15 Sample Code [ Lecture_8.java ]Lecture_8.java [ Output From Running Class Lecture_8 ]Output From Running Class Lecture_8


Download ppt "Lecture 8 February 29, 2000. Topics Questions about Exercise 4, due Thursday? Object Based Programming (Chapter 8) –Basic Principles –Methods –Fields."

Similar presentations


Ads by Google