Database Reference
In-Depth Information
We say that member variable names point to instances. At any time, we can point a member
variable name at another instance. In hardware, this actually works out to pointing the member at an
address in memory. When we point the member at null , we are saying that it doesn't point to any
address in memory.
Garbage Collection
A Java application in computer memory can be pictured as a basket full of Java objects, all referencing
one another, calling each other's methods and returning data, and getting and setting one-another's
members. In Java, we do not have to manually keep track of what objects are in use and release the
memory of unused objects, the Java runtime does that through a process called garbage collection.
When an object is no longer referenced by any other object in the basket, it falls out of the basket. It
doesn't disappear until the periodic garbage collection process walks through memory and sweeps the
fallen objects away.
Primitives
In addition to objects (described previously), we have Java primitives. Some of the primitive types are
int (integer), byte , and char (character). Member variables can be of types from both the Java classes
(objects) and the primitives (values).
Primitives do not have any methods and do not have any members. You can have arrays of
primitives, just like arrays of Java objects.
There are also Java classes that encapsulate the primitives, letting us hold primitives in an object
instance and retrieve primitives from an instance. For example, Integer , is a class that can encapsulate
an int . From Integer , we can get the int value of a String by calling Integer.parseInt( String s ) .
Some Java constructs can only handle objects, not primitives; for example, a Vector (a dynamic
array object), so we will need to encapsulate our primitive values if we intend to store them in a Vector .
Strings
A String is an immutable Java object, meaning you can't change it; however, you can replace it with
(point your member variable at) a new String . String has methods that look like you are changing the
object, but that is not the case. For example, you might call the String.toUpperCase() method:
myString.toUpperCase();
This invocation of toUpperCase() does not change the value of myString at all, but it has a return
type of String , and what gets returned is the value of myString in uppercase. The only way to set
myString to uppercase is to set it equal to the return value. In effect, you are setting myString to point at a
new uppercase String that is created by the toUpperCase() method:
myString = myString.toUpperCase();
A very common error when handling Strings is to try to compare them like you can do with the
primitive types, using equal signs for instance:
if( stringOne == stringTwo )
This is frighteningly valid code, but does not accomplish the test you intend. It is as bad as
accidentally using one equals sign in a test like this:
if( int1 = int2 )
 
Search WWH ::




Custom Search