Unformatted text preview:

Object ReferencesObjectsWhat this talk is aboutComputer memory organizationPrimitives in memoryObjects in memoryDeclaring and creating objectsPointers and referencesConsequences I: AssignmentConsequences II: parametersPrimitive parameters are copied inObject references are copied inConsequences III: finalConsequences IV: NullPointerExceptionConsequences V: EqualityArraysThe EndObject ReferencesObjects•An array is a collection of values, all of the same type•An object is a collection of values, which may be of different types–Actually, there’s more to an object than just values, but that’s all we need to consider right now•Example: class Person { String name; boolean male; int age; int phoneNumber;}•Notice that different kinds of objects typically take up different amounts of space in the computerWhat this talk is about•We’re going to discuss how objects are stored in computer memory•You might think this doesn’t matter to you, because Java just automatically does all the work of finding a place for objects and putting them there–You would be wrong•A lot of things involved in creating and manipulating objects are just plain mysterious unless you know what is going on “under the hood”Computer memory organization•A bit is a single on/off, yes/no, 0/1 value•A byte is eight consecutive bits•Computer memory is a numbered sequence of bytes, starting at zero–It is impossible for a memory location to be “empty”--bits are always either on or off•The number associated with a byte is called its address01101010010101101010111001111100010011101110100001101011011011100111100001010010111111110000000100020003000400050006000700080009000A000B01101110•••••••Addresses are binary numbers (usually written as hexadecimal), but this isn’t really relevant to the discussion•Larger computer systems might address words rather than bytes, where a word is some larger number of bits (such as 32 or 64)Primitives in memory•When you declare a primitive variable, you allocate space in memory for it•A byte requires one byte of memory•Hence, every variable has an address•Address 0000 is never used01101010010101101010111001111100010011101110100001111000010100101111111100000001000200030004000500080009000A000B01101110••••••01101011011011100006000701101011011011100006char c;byte b;010101100001int i;011110000101001011111111000801101110• An int requires four consecutive bytes of memory• A char requires two consecutive bytes of memoryObjects in memory•When you declare an object variable, Java does not know how much space the object needs--so it can’t allocate space for it•It doesn’t know how much space is needed until you assign an object to the variable•Instead, what Java does is allocate space for a pointer to the object–A pointer is a variable that holds an address–A pointer requires four consecutive bytes–The pointer is initially null (0000)•Later, when you assign an actual object to the variable, what Java does is to make the pointer “point to” (hold the address of) the memory occupied by the object01101010010101101010111001111100010011101110100001101011011011100111100001010010111111110000000100020003000400050006000700080009000A000B01101110••••••?Declaring and creating objects Person p = new Person("John");Person p allocates space to hold a reference (pointer) to a Personp:new Person("John") allocates space for the actual Person, and initializes it"John"07A3The assignment makes p refer to the new Person07A3Pointers and references•Java uses references rather than pointers•Both mean: A variable that holds an address–You can follow or dereference either one to get to the thing that it points to–You can assign either to a variable of the correct type•But you can do more with pointers:–You can treat a pointer as an integer and do arithmetic to get a new pointer–Pointers can point to anything; references refer to known objects•Consequences:–Pointers are much more flexible–References allow the computer to do more for you–90% of the serious errors in C and C++ programs are pointer errorsConsequences I: Assignment•When you assign to a primitive variable, you get a copy of the value•Hence, two object variables can refer to the same object–You can think of them as aliases of each other–If you change the object pointed to by one of them, you are changing the same object pointed to by the other24x3yy = x;24baJohn Smithtrue235551212b = a;a.age++;24• When you assign to an object variable, you get a copy of the referenceConsequences II: parameters•We call a method like this: result = add( 3 , 5 ) ;•Here’s the method: int add ( int number1, int number2 ) { int sum = number1 + number2 ; return sum ;}Actual parameter values are copied into formal parametersFormal parameters are used in methodA result is returnedPrimitive parameters are copied in int m = 3;int n = 5;result = add( m , n ) ; int add ( int number1 , int number2 ) { while (number1 > 0) { number1--; number2++; } return number2 ;}The values of m and n (that is, 3 and 5) are copied to the formal parametersChanging number1 and number2 does not affect m and n• This is call by valueThe value of number2 is returned, but the variable number2 is thrown awayObject references are copied in Person p = new Person("John");changeName( p ); void changeName(Person per ) { per.name = "Jack";}• This means that p and per refer to the same object!• This is call by referencep holds a reference to a Person"John"It is the reference that is copied in, not the Person• p is not changed, but changes made to the object referenced by per remain changed when the method returns"Jack"per.name = "Jack";Consequences III: final•When you declare a variable to be final, you cannot change its value: final int speedLimit = 65; speedLimit = 70; // illegal!•The value of an object variable is the address (reference) it contains, not the object itself: final Person president = new Person("George Bush"); president = new Person("Al Gore"); // illegal president.age = president.age + 1; // legal! president.name = "Al Gore"; // legal!Consequences IV: NullPointerException•When you declare an object variable, but before you assign it a value, it has the value null (0000)•null is a legal value for any object variable–You can test if a variable has value null–You can assign null like


View Full Document

Penn CIS 121 - Object References

Download Object References
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 Object References 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 Object References 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?