DOC PREVIEW
UT CS 307 - Topic 3 References and Object Variables

This preview shows page 1-2-14-15-29-30 out of 30 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Topic 3 References and Object Variables"Thou shalt not follow the NULLThou shalt not follow the NULL pointer, for chaos and madness await th t it d "thee at its end."-Henry SpencerHenry SpencerCS 307 Fundamentals of Computer Science References and Object Variables1Object Variables8object 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.h th API•show the API page– don't learn the classes, learn how to read and use a class interface (the users manual)()8objects 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 ScienceReferences and Object Variables2Object Variablespublic void objectVaraiables()public void objectVaraiables(){ Rectangle rect1;Rectangle rect2;// 2 Rectangle objects exist??// 2 Rectangle objects exist??// more code to follow}8So now there are 2 Rectangle objects right?8Not so much.8Object 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 variablesCS 307 Fundamentals of Computer ScienceReferences and Object Variables3–contrast this with primitive variablesThe Pointer Sidetrack8IMPORTANT!!This material may8IMPORTANT!! This material mayseem a bit abstract, but it is often thecause of many a programmers logic error8A pointer is a variable that stores thememory address of where anothervariable is storedaabesstoed8In some languages you can have bound variables and dynamic variables of any typeb d ibl i thti itd ith–a bound variable is one that is associated with a particular portion of memory that cannot be changed8Example C++, can have an integer variable or a i t i t ( hi h i till i bl )integer pointer (which is still a variable)int intVar; // a int varint * intPtr; //pointer to an int varCS 307 Fundamentals of Computer ScienceReferences and Object Variables4ii 5//iPointer Variables in C++int intVar = 5; // a int varint * intPtr; //pointer to an int varintPtr = new int; /* d i ll ll i/* dynamically allocate an space to store an int. intPtr holds the memory address of this space*/5????0x00122155itVitPt??space for an int in??intVarintPtrspace for an int in memory assume memory ddCS 307 Fundamentals of Computer ScienceReferences and Object Variables5address 0x00122155Pointer Complications8C++ allows actual variables and pointers to variables of any type. Things get complicated and confusing very quicklyititV 5 // itint intVar = 5; // a int varint * intPtr; //pointer to an int varintPtr = new int; // allocate memory*intPtr = 12; /* assign the integer being*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*/the thing being pointed at /cout << intPtr << "\t" << *intPtr << "\t"<< &intPtr << endl;// 3 different ways of manipulating intPtr8In C++ you can work directly with the memory address stored in intPtr// 3 different ways of manipulating intPtrCS 307 Fundamentals of Computer ScienceReferences and Object Variables6– increment it, assign it other memory addresses, pointer “arithmetic”Attendance Question 1Given the following C++ declarations how would the variable intPtr be made to refer tth ibl?to the variable intVar?intVar = 5;intPtr = new int;tt e t;A. intPtr = intVar;BintPtr = *intVar;B. intPtr = *intVar;C. *intPtr = intVar;D. intPtr = &intVar;E. intPtr = intVar;CS 307 Fundamentals of Computer ScienceReferences and Object Variables7And Now for Something Completely DifferentCompletely Different…8Thanks Nick…8Link to BinkCS 307 Fundamentals of Computer ScienceReferences and Object Variables8Benefit of Pointers8Why have pointers?Why have pointers?8To allow the sharing of a variable–If several variables(objects, records, structs) need accessIf 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 dataptrptrotherdatathotherdatanot shownshared variableh2pnot shownnot shownCS 307 Fundamentals of Computer ScienceReferences and Object Variables9shared variablesharer 2sharer 1Time Space Trade Off88Often 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.spaceusedBADusedGOODtime to completeGOODCS 307 Fundamentals of Computer ScienceReferences and Object Variables10time to completeMore Benefits8Allow dynamic allocation of memory8Allow dynamic allocation of memory– get it only when needed (stack memory and heap memory) 8Allow linked data structures such as linked lists and binary treesidibl flf tit fbl–incredibly useful for certain types of problems8Pointers are in fact necessary in a language like Java wherepolymorphismis so prevalent (more onJava where polymorphism is so prevalent (more on this later)8Now 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 ScienceReferences and Object Variables11simplifications in the languageDynamic Memory AllocationYour program has two chunks of memory to workwith: Stack memory (or the runtime Stack) andHeap memoryHeap memoryWhen a Java program starts it receives two chunkspgof memory one for the Stack and one for the Heap.Things that use Stack memory: local variablesThings that use Stack memory: local variables, parameters, and information about methods that arein progress.Things that use Heap memory: everything that isallocated using thenewoperatorCS 307 Fundamentals of Computer ScienceReferences and Object Variables12allocated using the newoperator.The PictureStack MemoryHeap MemoryStack MemoryHeap MemoryString ObjectxsgjmyCharsyH e l l ovoid toyCodeForMemory(int x){ int y = 10;{y;x += y;String s = new String("Hello");System.out.println(x + " " + y + s);CS 307 Fundamentals of Computer ScienceReferences and Object Variables13Syste .out.p t ( y s);}How Much Memory?How big is the Heap?gpSystem.out.println("Heap size is " + Runtime.getRuntime().totalMemory()); How much of the Heap is available?System.out.println("Available memory: " + yp(


View Full Document

UT CS 307 - Topic 3 References and Object Variables

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 Topic 3 References and Object Variables
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 Topic 3 References and Object Variables 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 Topic 3 References and Object Variables 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?