|
You need remember following things in particular about the String and StringBuffer objects.
1. The String class overrides the equals() method from the Object class, but the StringBuffer class do not override it. If you invoke the equals() method on String object, it returns true if argument is of type String and the contents of the argument string are same as the String object. But if you invoke equals() on a StringBuffer object, the equals() returns true only when you pass reference to essentially the same StringBuffer object. In simple words, two StringBuffer objects are not equal just because their contents are equal. For example,
StringBuffer sbuf1 = new StringBuffer("test");
StringBuffer sbuf2 = new StringBuffer("test");
System.out.println(sbuf1.equals(sbuf2)); // prints false
System.out.println(sbuf1.equals(sbuf1)); // prints true
Thus the equals() method in StringBuffer class essentially does the identity test because it returns true only when the two objects are essentially same.
2. If you pass a StringBuffer object as a arguments for String class’s equals() method or vice versa. The result is always false. You may recall that the equals() method in String first checks whether argument passed is of type String (using the instanceof operator). If not, it returns false. Therefore, following example shows that a StringBuffer object is not equal to a string object even when their contents are same.
StringBuffer sbuf = new StringBuffer("test");
String str = "test";
System.out.println(str.equals(sbuf)); // prints false
System.out.println(sbuf.equals(str)); // prints false
Remember, the StringBuffer class and the String class, are two independent classes that are not related by inheritance. Hence, if you use instanceof on String object to check whether it is of type StringBuffer or vice versa, you will always get false as a result.
3. Usually methods are invoked on the reference variable. But sometimes a method can be invoked on another method call. Such method invocation is called as the chained invocation. For instance,
someVar.method1().method2();
Here the method2() is directly invoked on the result returned by the call someVar.method1().
|
|
You may find chained method invocations in the question based on strings as it makes the code look more confusing. |
When you encounter a chained method invocation, you can simplify it with the help of following steps.
1. Start evaluating the method calls from left to right.
2. Determine what the leftmost method call will return.
3. Use the returned object as the object invoking the second (from the left) method.
Repeat these steps (1 to 3) until you get the final result.
For example,
String str = "Jack and Jill ";
System.out.println(str.concat("went up the hill ").replace('i','a'));
The methods are invoked in chained fashion. If you follow the steps from left to right, you can easily evaluate the result. First you need to find out the result of left most method str.concat(..) , which will be “Jack and Jill went up the hill”. Now on this string object, the next method call replace() will be invoked. You will get “Jack and Jall went up the hall” as the result.