Data Objects (revisited) Recall that values are stored in data objects, and that each data object...
-
Upload
gwen-harvey -
Category
Documents
-
view
226 -
download
2
Transcript of Data Objects (revisited) Recall that values are stored in data objects, and that each data object...
Data Objects (revisited)
Recall that values are stored in data objects, and that
each data object holds one value of a particular
type. Data objects may be labeled with names –
variables. The variable refers to the data object,
and when we set the variable to a value, we replace
the value in the data object. When we use the
variable in an expression, we retrieve the value
stored in the data object.
References
For primitive types (int, char, etc.) we can think of
the variable has storing the value of the particular
type. However, this is different for object
variables. An object variable contains a reference
to the data object where the values are actually
stored. This is an importance difference.
An Analogy
You live in a house. The house has an address. The
address is not the same as the house, but can be
used to refer to the house. If you say 'so-and-so'
lives at 1313 Mockingbird Lane you really mean
that 'so-and-so' lives at the house at 1313
Mockingbird Lane. We often confuse the address
with the actual house. This is not a problem for
human beings.
Analogy (cont'd)
Similarly, data objects have addresses (for example,
the address could be the address of the memory
location in RAM where the data object is
electronically stored. The address is not the same
as the data object, but can be used to refer to the
data object and its contents.
So What?
A primitive variable can be thought to hold a value
of its type. But an object variable holds the
address (reference) to the data object of its type.
The program will automatically fetch the data object
when you use the variable, i.e., go the house and
get (or replace) the person there, but the variable
really holds the address.
Still, So What?
Here's the point:
Suppose that we create an object, say a Snowman:Snowman steve = new Snowman();
and then we set another variable to have the same
value:Snowman x = steve;
Then steve and x hold the same address, that is, they
refer to the same data object.
This is What
Changing the properties of the Snowman that the
variable steve refers to also changes the properties
of the Snowman that x refers to (since they are the
same Snowman or data object). So,
steve.setColor("red");System.out.prinln(x.getColor());
would print out "red".
Changing Variables
It is important to note that changing the properties of
the object is different than changing the variable.
For example,
Snowman dave = new Snowman();x = dave;
would make x refer to the same Snowman the dave
refers to.
Summary
Object variables hold references to (addresses of)
data objects. If two object variables refer to the same data
object, changing the properties of the data object
affects both. An assignment statement can be used to make an
object variable point to a different data object.
Parameters
This difference applies to parameters as well.
If a parameter is a primitive type, its value is
initialize to be the value of the corresponding
argument (in the call to the method). The value is
copied into the data object which that variable
names.
Parameters (cont'd)
If a parameter is an object type, the value of the
argument is also copied to the parameter. But since
the value is a reference to the data object, both the
parameter and the argument refer to the same
data object.
Changing Values
If you change the value of a parameter of a primitive
type in the method, it has no effect on the value of
the argument, since the parameter has a copy of
that value.
But, if you change the properties of the data object
that a parameter of object type refers to, it changes
the properties of the data object that the argument
refers to, since they are the same data object.
Example – Primitive type parameter
public class Foo { public static void incr(int x) { x = x + 1; }}
...
In main:int n = 5;Foo.incr(n); // n doesn't change.
Example Object Type Parameter
public class Foo { public static void change(Snowman x) { x.changeColor("red"); }}
...
In main:Snowman steve = new Snowman()Foo.change(steve); // steve changes color
Exercise
Write a program, Fraud.java, that allows one to
change the balance in their bank account, even
though the balance field is private.
Solution
To avoid allowing one to change the properties of an
object (such as balance), a common trick is to
return a copy of the object. Thus changing the copy
does not change the original.
A copy constructor is often used to make copies of
the object. It is a one-argument constructor that
takes an object and copies the fields to a new
object.
Solution (cont'd)
Remember that if fields of the object are themselves
objects (example: the snowman's head), then those
fields need to be copied as well.
Other pitfalls
The equality operator (==) checks if two objects are
the same, that is, they are the same data object (in
memory). It does not check that they may be two
different objects, but having the same properities,
i.e., account number, balance, APR.
The equals method should be used instead. This
method should compare the fields for equality.
The Constant null
The constant null is a special constant that may be
assigned to a variable of any class type. It does not
refer to any object – and that's the point. When we
have a variable that doesn't refer to anything, we
set it to null. It is a placeholder. For example, if a
person did not have a bank account, their
bankaccount field would be set to null.
Null (cont'd)
This may create a problem. It means that we have to
check that an object variable is not null before
using the object it refers to. So, many of our
methods (equals, compareTo) should check that the
argument is not null before trying to process it.
If you don't, you may get the dreaded
Null Pointer Exception
Packages
A package is how we can form libraries of classes. A
package is a collection of classes that has been
grouped together in a directory and given a
package name.
A package is made available to another program by
using the import statement. For example, import java.util.Scanner;
will import one class of a package.
Packages (cont'd)
We can import all the classes of a package by using a
wildcard:
import java.util.*;
The import statement must come at the top of the program.
Creating Package
To create a package, we put all of the files into one
directory (and where the directory is will be
important) and add a package line to each file:
package java.util;
This line must come at the beginning of the file.
Package Names and Directories
The name of the package is part of the path name to
the directory containing the classes of the package.
To find a package, Java needs two things – the
CLASSPATH variable and the name of the
package.
The CLASSPATH variable is an environmental
variable that gives the base location to find
packages.
Path Names
A path name is list of (nested) directories that is used
to locate files. In Unix, we use the forward slash
“/” to separate the directories, and in Windows, the
backward slash, “\”. Unix:
/libraries/newlibraries/utilities/numericstuff
In Windows:
\libraries\newlibraries\utilities\numericstuff