DOC PREVIEW
UW-Madison CS 302 - Primitive vs. Reference types

This preview shows page 1 out of 2 pages.

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

Unformatted text preview:

Primitive vs. reference typeshandout for CS 302 by Will Benton (willb@cs)Recall that a variable is a named place to store a value, and that every variable has a type, or range of values that it may hold. Some types are called primitive types. These types are built in to the Java language and encompass ranges of whole numbers (int, long, short, and byte), numbers with fractional parts (float and double), individual characters (char), and truth values (boolean). A variable with a primitive type contains a value within the range of that primitive type.Some other types are called reference types. Reference types include class types, interface types (discussed in chapter 11), and array types (discussed in chapter 8). Reference types are so called because a variable with a reference type will contain a reference to (or address of) an object. A variable with a reference type will contain a reference to a particular kind of object or array. Note that such a variable will not contain an actual object or array.Think about why this difference is important. Why do reference variables hold references to objects instead of actual objects?int a = 5;int b = a;/* P1 */Integer w = new Integer(a);Integer x = w;/* P2 */Clicker c = new Clicker();Clicker d = new Clicker();Clicker e = c;/* P3 */The dialogue at right refers to the following program fragment:What is the result of the statement a = a + 1; ? It increments the value stored in a by 1.What would happen if I added the above statement to the program at the point marked P1?a would contain the value 5 before that statement executed and 6 afterwards. Note that the value of b would not change.w is a reference variable. Do you know how to tell?What kind of variable is w?Yes, and the value that w gets is constructed with operator new, which always returns a reference. Is there still another way to tell?Well, I know it is of type Integer, and that's not a primitive type.Yes! Since Integer is capitalized, I can be pretty sure that it is a class name, as long as the programmer is following the Java naming conventions and not trying to trick me.That's right! Of course, you know that Integer is a type because of where it appears in the line of code. You know that it isn't a primitive type, since there are only a few of those to remember; and you know it's not an array, since we haven't learned about them yet. Therefore, even if the programmer is being deliberately tricky, we can be fairly certain that Integer is a reference type, and that a variable of type Integer holds a reference to an object.What is the value of w? w contains a reference to an Integer object. No, seriously, what is the value of w? Seriously, w contains a reference to an Integer object. The particular Integer object that w contains a reference to "wraps" (or represents) the value 5.Yikes! How can we notate that? Java programmers will often use memory diagrams to talk about objects, references, and variables.Can you show me an example? Sure. This diagram shows the value of w after it's initialized:wInteger...I assume that the diagram means that w points to an instance of class Integer. What's up with the ellipsis? Are you hiding details from me?You assume correctly! The ellipsis is just there to save space. Usually when we make a memory diagram, we'll show the instance fields of each individual object. In this case, we don't know what the instance fields of Integer are, but we have an idea that the Integer pointed to by w might "wrap" the int value 5.What is the result of the statement Integer x = w; ?It declares a new variable of type Integer called x. Furthermore, it initializes x to contain the same value as that stored in w.What does it mean for "x to contain the same value as that stored in w?"In the case of reference variables, it means that x and w refer to the same object. (If x and w were primitive-type values, each would have its own copy of some value.)primitive-vs-reference.graffle: Created on Sun Jun 25 2006; modified on Sun Jun 25 2006; page 1 of 2Here is the source code for the instantiable Clicker class:It will produce the following output:You're right. The memory diagram would look like this:What does it mean for "x to contain the same value as that stored in w?"In the case of reference variables, it means that x and w refer to the same object. (If x and w were primitive-type values, each would have its own copy of some value.)primitive-vs-reference.graffle; page 2 of 2A ha! This is sort of like how I can refer to the same real-world place by saying "1210 W. Dayton Street" and "the Computer Sciences building at UW-Madison." How would we notate this relationship in a memory diagram?wInteger...xpublic class Clicker { private int ct; public Clicker() { ct = 0; } public void click() { ct = ct + 1; } public void reset() { ct = 0; } public int inspect() { return ct; }}Here is the source code for the instantiable Clicker class:What is a Clicker object for? It models an incrementing counter, like one that an usher at a concert venue or a cinema might use.What happens to a Clicker object when you send it a click() message?Its count, as stored in its ct field, is incremented by one. You can inspect the count of a Clicker object by sending it an inspect() message.By the way, "instantiable" isn't in any dictionary I can find, and it makes my spell-checker complain. Could it possibly be a real word?Your textbook certainly thinks so.Let's say that I insert the following lines of code into the program at the point labeled P3:System.out.println(c.inspect());System.out.println(d.inspect());System.out.println(e.inspect());c.click();System.out.println(d.inspect());System.out.println(e.inspect());00001Can you figure out why?What output would the program produce?cClickerct = 0dClickerct = 0eWell, at point P3, memory looks like so, with c and e pointing to the same Clicker:Right, so it's not surprising that changing the ct field of the Clicker pointed to by c (by sending the click() message) would change the ct field of the Clicker pointed to by e (that we access by sending an inspect() message). Since we're referring to the same object by two different names, there's some potential for confusion!cClickerct = 1dClickerct = 0eI'll have to be very careful when I have multiple references to the same object.I couldn't have said it better myself.We'll use the following symbols in our memory diagrams:oreference variable,


View Full Document

UW-Madison CS 302 - Primitive vs. Reference types

Download Primitive vs. Reference types
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 Primitive vs. Reference types 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 Primitive vs. Reference types 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?