Java Reference
In-Depth Information
By default, the class definition is loaded only once, and there is only one Class object per Java class. You are
not considering those cases where you have written code to load the same class more than once. If you use the
getClass() method on different objects of the same class, you will get the reference of the same Class object.
Consider the following snippet of code:
Cat c2 = new Cat();
Cat c3 = new Cat();
Class catClass2 = c2.getClass();
Class catClass3 = c3.getClass();
Here, c2 and c3 are two objects of the same Cat class. Therefore, c2.getClass() and c3.getClass() return
the reference of the same Class object, which represents the Cat class in the JVM. The expression catClass2 ==
catClass3 will evaluate to true .
The Class class has many useful methods. I discuss most of its methods in Chapter 3 of the topic Beginning Java
Language Features . You can use its getName() method to get the fully qualified name of the class. You can use its
getSimpleName() to get the simple name of the class. For example,
String fullName = catClass.getName();
String simpleName = catClass.getSimpleName();
not all classes in an application are loaded into jVM when the application starts. a class is loaded and a cor-
responding Class object is created when the application uses the class for the first time.
Tip
Computing Hash Code of an Object
A hash code is an integer value that is computed for a piece of information using an algorithm. A hash code is
also known as a hash sum, a hash value, or simply a hash. The algorithm to compute an integer from a piece of
information is called a hash function.
The definition of a hash code involves three things:
A piece of information
An algorithm
An integer value
You have a piece of information. You apply an algorithm to it to produce an integer value. The integer value that
you get is the hash code for the piece of information you had. If you change the piece of information or the algorithm,
the computed the hash code may or may not change. Figure 7-1 depicts the process of computing the hash code.
A piece of
information
An integer
(hash code)
Algorithm
Figure 7-1. Process of computing a hash code
 
 
Search WWH ::




Custom Search