My question is why the following statement: System.out.println(stringbuffer.equals(string));
returns false (assuming stringbuffer and string are valid StringBuffer and String objects respectively). Instead, shouldn't it give a compiler error because the equals method in StringBuffer performs == operation, and == gives compiler error when objects are of different type?
My guess is - the compiler gives error when it is evident the classes are of incomparable types. But in equals one of them get up-casted to Object reference and it doesn't give compiler error. I noticed that if you cast the compared objects to Object while using == operator it doesn't give error for incomparable types. So it might be only compile time check of the object reference type not the actual type of objects. Someone please confirm this?! [ August 28, 2007: Message edited by: B Misra ]