Unformatted text preview:

Topic 3 References and Object VariablesObject VariablesSlide 3The Pointer SidetrackPointer Variables in C++Pointer ComplicationsAttendance Question 1And Now for Something Completely Different…Benefit of PointersTime Space Trade OffMore BenefitsDynamic Memory AllocationThe PictureHow Much Memory?References in JavaBack to the Rectangle ObjectsCreating ObjectsThe Yellow Sticky AnalogyPointers in JavaWorking with ObjectsWhat's the Output?Attendance Question 2Attendance Question 3Attendance Question 4Attendance Question 5Equality versus IdentityJust Like the Real WorldThe Garbage CollectorObjects as ParametersImmutable ObjectsCS 307 Fundamentals of Computer Science References and Object Variables1Topic 3 References and Object Variables"Thou shalt not follow the NULL pointer, for chaos and madness await thee at its end."- Henry SpencerCS 307 Fundamentals of Computer Science References and Object Variables2Object Variablesobject variables are declared by stating the class name / data type and then the variable name–same as primitives–in Java there are hundreds of built in classes.•show the API page–don't learn the classes, learn how to read and use a class interface (the users manual)objects are complex variables. –They have an internal state and various behaviors that can either change the state or simply tell something about the objectCS 307 Fundamentals of Computer Science References and Object Variables3Object VariablesSo now there are 2 Rectangle objects right?Not so much.Object variables in Java are actually references to objects, not the objects themselves!–object variables store the memory address of an object of the proper type not an object of the proper type.–contrast this with primitive variablespublic void objectVaraiables(){ Rectangle rect1;Rectangle rect2;// 2 Rectangle objects exist??// more code to follow}CS 307 Fundamentals of Computer Science References and Object Variables4The Pointer SidetrackIMPORTANT!! This material mayseem a bit abstract, but it is often thecause of many a programmers logic errorA pointer is a variable that stores thememory address of where anothervariable is storedIn some languages you can have bound variables and dynamic variables of any type–a bound variable is one that is associated with a particular portion of memory that cannot be changedExample C++, can have an integer variable or a integer pointer (which is still a variable)int intVar; // a int varint * intPtr; //pointer to an int varCS 307 Fundamentals of Computer Science References and Object Variables5int intVar = 5; // a int varint * intPtr; //pointer to an int varintPtr = new int; /* dynamically allocate an space to store an int. intPtr holds the memory address of this space*/Pointer Variables in C++intVar5intPtr??space for an int in memory assume memory address 0x00122155??0x00122155CS 307 Fundamentals of Computer Science References and Object Variables6Pointer ComplicationsC++ allows actual variables and pointers to variables of any type. Things get complicated and confusing very quicklyIn C++ you can work directly with the memory address stored in intPtr–increment it, assign it other memory addresses, pointer “arithmetic” int intVar = 5; // a int varint * intPtr; //pointer to an int varintPtr = new int; // allocate memory*intPtr = 12; /* assign the integer being pointed to the value of 12. Must dereference the pointer. i.e. get to the thing being pointed at*/cout << intPtr << "\t" << *intPtr << "\t"<< &intPtr << endl;// 3 different ways of manipulating intPtrCS 307 Fundamentals of Computer Science References and Object Variables7Attendance Question 1Given the following C++ declarations how would the variable intPtr be made to refer to the variable intVar?intVar = 5;intPtr = new int;A. intPtr = intVar;B. intPtr = *intVar;C. *intPtr = intVar;D. intPtr = &intVar;E. intPtr = intVar;CS 307 Fundamentals of Computer Science References and Object Variables8And Now for Something Completely Different…Thanks Nick…Link to BinkCS 307 Fundamentals of Computer Science References and Object Variables9Benefit of PointersWhy have pointers?To allow the sharing of a variable–If several variables(objects, records, structs) need access to another single variable two alternatives1. keep multiple copies of variable. 2. share the data with each variable keeping a reference to the needed datashared variablesharer 2sharer 1ptrptrotherdatanot shownotherdatanot shownCS 307 Fundamentals of Computer Science References and Object Variables10Time Space Trade OffOften the case that algorithms / solutions an be made faster by using more space (memory) or can use less space at the expense of being slower.spaceusedtime to completeGOODBADCS 307 Fundamentals of Computer Science References and Object Variables11More BenefitsAllow dynamic allocation of memory–get it only when needed (stack memory and heap memory) Allow linked data structures such as linked lists and binary trees–incredibly useful for certain types of problemsPointers are in fact necessary in a language like Java where polymorphism is so prevalent (more on this later)Now the good news–In Java most of the complications and difficulties inherent with dealing with pointers are removed by some simplifications in the languageCS 307 Fundamentals of Computer Science References and Object Variables12Dynamic Memory AllocationYour program has two chunks of memory to workwith: Stack memory (or the runtime Stack) andHeap memoryWhen a Java program starts it receives two chunksof memory one for the Stack and one for the Heap.Things that use Stack memory: local variables, parameters, and information about methods that arein progress.Things that use Heap memory: everything that isallocated using the new operator.CS 307 Fundamentals of Computer Science References and Object Variables13The PictureStack Memory Heap Memoryvoid toyCodeForMemory(int x){ int y = 10; x += y; String s = new String ("Hello"); System.out.println(x + " " + y + s);}xysString ObjectmyCharsH e l l oCS 307 Fundamentals of Computer Science References and Object Variables14How Much Memory?How big is the Heap?System.out.println("Heap size is " + Runtime.getRuntime().totalMemory()); How much of the Heap is available?System.out.println("Available memory: " + Runtime.getRuntime().freeMemory());CS 307 Fundamentals of Computer Science References and Object Variables15References in JavaIn


View Full Document

UT CS 307 - Study Notes

Documents in this Course
Midterm 2

Midterm 2

15 pages

Midterm 1

Midterm 1

15 pages

Syllabus

Syllabus

24 pages

s

s

8 pages

Midterm 1

Midterm 1

14 pages

Midterm 2

Midterm 2

14 pages

Recursion

Recursion

14 pages

Midterm 1

Midterm 1

16 pages

Load more
Download Study Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?