All the rar files have the same 
password : http://learning4you.blogspot.com/

Monday, November 10, 2008

Comparing objects,Finding out the class of any given object

  • Comparing objects
  • Finding out the class of any given object
  • Testing to see whether an object is an instance of a given class

Comparing Objects

Yesterday you learned about operators for comparing values: equals, not equals, less than, and so on. Most of these operators work only on primitive types, not on objects. If you try to use other values as operands, the Java compiler produces errors.

The exception to this rule is with the operators for equality: == (equal) and != (not equal). These operators, when used with objects, test whether the two operands refer to exactly the same object in memory.

What should you do if you want to be able to compare instances of your class and have meaningful results? You have to implement special methods in your class, and you have to call those methods using those method names.

Technical Note
Java does not have the concept of operator overloading-that is, the ability to redefine the behavior of the built-in operators using methods in your own classes. The built-in operators remain defined only for numbers.

A good example of this is the String class. It is possible to have two strings, two independent objects in memory with the same values-that is, the same characters in the same order. According to the == operator, however, those two String objects will not be equal, because, although their contents are the same, they are not the same object.

The String class, therefore, defines a method called equals() that tests each character in the string and returns true if the two strings have the same values. Listing 4.5 illustrates this.


Listing 4.5. A test of string equality.
 1: class EqualsTest {
2: public static void main(String args[]) {
3: String str1, str2;
4: str1 = "she sells sea shells by the sea shore.";
5: str2 = str1;
6:
7: System.out.println("String1: " + str1);
8: System.out.println("String2: " + str2);
9: System.out.println("Same object? " + (str1 == str2));
10:
11: str2 = new String(str1);
12:
13: System.out.println("String1: " + str1);
14: System.out.println("String2: " + str2);
15: System.out.println("Same object? " + (str1 == str2));
16: System.out.println("Same value? " + str1.equals(str2));
17: }
18: }

String1: she sells sea shells by the sea shore.
String2: she sells sea shells by the sea shore.
Same object? true
String1: she sells sea shells by the sea shore.
String2: she sells sea shells by the sea shore.
Same object? false
Same value? true

Analysis
The first part of this program (lines 4 through 6) declares two variables (str1 and str2) assigns the literal she sells sea shells by the sea shore. to str1, and then assigns that value to str2. As you learned earlier when we talked about object references, now str1 and str2 point to the same object, and the equality test at line 10 proves that.

In the second part, you create a new string object with the same value as str1 and assign str2 to that new string object. Now you have two different string objects in str1 and str2, both with the same value. Testing them to see whether they're the same object by using the == operator (line 16) returns the expected answer (false-they are not the same object in memory), as does testing them using the equals() method (line 17) (true-they have the same values).

Technical Note
Why can't you just use another literal when you change str2, rather than using new? String literals are optimized in Java-if you create a string using a literal, and then use another literal with the same characters, Java knows enough to give you the first String object back. Both strings are the same objects-to create two separate objects you have to go out of your way.

Determining the Class of an Object

Want to find out the class of an object? Here's the way to do it for an object assigned to the variable obj:
String name = obj.getClass().getName();
What does this do? The getClass() method is defined in the Object class, and as such is available for all objects. The result of that method is a Class object (where Class is itself a class), which has a method called getName(). getName() returns a string representing the name of the class.

Another test that might be useful to you is the instanceof operator. instanceof has two operands: an object on the left and the name of a class on the right. The expression returns true or false based on whether the object is an instance of the named class or any of that class's subclasses:

"foo" instanceof String // true
Point pt = new Point(10, 10);
pt instanceof String // false
The instanceof operator can also be used for interfaces; if an object implements an interface, the instanceof operator with that interface name on the right side returns true

0 comments:

Related Posts with Thumbnails