Page 120 - Introduction to Programming with Java: A Problem Solving Approach
P. 120

                86 Chapter 3 Java Basics
There are additional primitive types (boolean, byte, short), which we’ll get to in Chapters 4
and 11, but for most situations these five primitive types are sufficient.
Reference Variables
Whereas a primitive variable stores a single piece of data, a reference variable stores a memory location that points to a collection of data. This memory location is not a literal memory address, like a street ad- dress. It’s a coded abbreviation, like a post-office box number. However, for everything you can do in Java, the value in a reference variable acts exactly like a literal memory address, so we’ll pretend it is one. We said a reference variable’s “address” points to a collection of data. More formally, it points to an object. You’ll learn about object details in Chapter 6, but for now, just realize that an object is a collection of related data wrapped in a protective shell. To access an object’s data, you need to use a reference variable (or reference for short) that points to the object.
String variables are examples of reference variables. A string variable holds a memory address that points to a string object. The string object holds the data—the string’s characters.
Reference variables are declared with a reference type. A reference type is a type that provides for the storage of a collection of data. String is a reference type, and it provides for the storage of a collection of characters. So in the following example, declaring name with a String reference type means that name points to the collection of characters T, h, a, n, h, space, N, g, u, y, e, n.
String name = "Thanh Nguyen";
String is just one reference type from among a multitude of reference types. Classes, arrays, and inter- faces are all considered to be reference types. You’ll learn about arrays in Chapter 10 and interfaces in Chapter 13. You’ll learn about clAasspdeatagilsoin ChPapDterF6, buEt fnorhnowa,nit’scgoeodrenough to know that a class is a generic description of the data in a particular type of object. For example, the String class describes the nature of the data in string objects. More specifically, the String class says that each string object can store zero or more characters and the characters are stored in a sequence.
An Example
Let’s look at an example that uses primitive variables and reference variables. In this code fragment, we declare variables that keep track of a person’s basic data:
int ssn;
String name;
// social security number
// person's name
Calendar bday; // person's birthday
As you can tell by the int and String data types, ssn is a primitive variable and name is a reference variable. In the third line, Calendar is a class. That tells us that bday is a reference variable. The Cal-
14
endar class allows you to store date information such as year, month, and day. Since bday is declared
withtheCalendar class,bdayisabletostoreyear,month,anddaydataitems. 3.22 Strings
We’ve used strings for quite a while now, but we’ve stored them and printed them and that’s it. Many pro- grams need to do more with strings than just store and print. For example, Microsoft Office programs
14 Explaining the Calendar class in depth is beyond the scope of this chapter. If you want an in-depth explanation, go to Sun’s Java documentation Web site (http://java.sun.com/javase/6/docs/api/) and search for Calendar.
  












































































   118   119   120   121   122