Java Reference
In-Depth Information
The compiler error looks like this:
inconvertible types
found : java.math.BigDecimal
required: java.lang.String
String s = (String) bd;
Even though s and bd are both references that behind the scenes are identical in terms
of memory consumption (most likely they are 32-bit unsigned integers, but this is JVM-
dependent), it is not possible to assign them to each other because there is no relationship
between a String object and a BigDecimal object. Two references are compatible only
when either the objects they point to are the same type or one of the objects is a child class
of the other. String and BigDecimal have no inheritance relationship.
Hopefully you have a better understanding of the differences between references and
primitive types. References play a key role in understanding garbage collection, our next
topic.
Garbage Collection
All Java objects are stored in your program memory's heap . The heap, which is also
referred to as the free store, represents a large pool of unused memory allocated to your
Java application. The heap may be quite large, depending on your environment, but there is
always a limit to its size. If your program keeps instantiating objects and leaving them on
the heap, eventually it will run out of memory.
Garbage collection refers to the process of automatically freeing memory on the heap by
deleting objects that are no longer reachable in your program. Every JVM has a garbage
collector, and many different algorithms are used to determine the effi ciency and timing of
garbage collection. The SCJP exam does not test your knowledge of any individual garbage
collection algorithm. However, you do need to know “what is and is not guaranteed by the
garbage collection system,” as well as “recognize the point when an object becomes eligible
for garbage collection.” This section discusses both of these objectives in detail.
The new keyword instantiates a new object on the heap and returns a reference to the
object. Typically you will save that object's reference in a variable. An object will remain on
the heap until it is no longer reachable. An object is no longer reachable when one of two
situations occurs:
The object no longer has any references pointing to it.
All references to the object have gone out of scope.
Search WWH ::




Custom Search