150 likes | 286 Views
Lecture 8. February 29, 2000. Topics. Questions about Exercise 4, due Thursday? Object Based Programming (Chapter 8) Basic Principles Methods Fields Memory Structures. Understanding Objects: Basic Principles. Need to know the difference between classes and objects.
E N D
Lecture 8 February 29, 2000
Topics • Questions about Exercise 4, due Thursday? • Object Based Programming (Chapter 8) • Basic Principles • Methods • Fields • Memory Structures
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.
Disk Computers and Virtual Machines Input Memory (RAM) Output Load Store Instructions and Data Results Processor (CPU)
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.
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.”)
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.”
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
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.
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.
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.
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).
An Object in Memory Static Field Class Pointer Reference Variable Static Field Static Field Instance Field Instance Field Instance Field method method Instance Field This part is created when the class is instantiated This part is created when the class file is loaded into memory
Sample Code • [ Lecture_8.java ] • [ Output From Running Class Lecture_8 ]