Class Presentation Summary for June 14, 1999 We discussed several issues concerning the use of Java operators. The first issue concerned the differences between the logical connectives, && and ||, and the bitwise operators & and |. You should be careful not to confuse them when writing or reading programs. The strong typing of Java helps to idenity mistakes in the use of these operators since they work on different types of data. Remember that ! is the logical not operator. Then we spent considerable time working through which operators could be applied to which primitive data types. In particular, we noticed that most operators could not be used with boolean data. Each primitive type can be cast to any of the others except that boolean cannot be cast to any other type and none of the other types can be cast to boolean. The most important operator consideration we discussed was the use of == and != with objects. Primitive values can be directly compared with these two operators whether the primitive value is a literal or an object field. Comparing two objects with either of these two operators gives an unexpected, but sometims useful result. Since objects are actually references ( refered to in the book as handles) to their data and control information, == yields true only when the two references being compared have the same values. To compare two objects with a truth result dependent on the values of the object data fields, we should use the equals method. If we wish to compare two objects, a and b of the same type, we could use a.equals(b) which returns true if and only if the data fields all have the same corresponding values. There is one last concern with this, however. equals is not defined properly for user-defined classes unless the user overrides the default definition. We looked at a simple example and students were given an in-class exercise to build an equals method for the class: public class joe{ char i; double j; } Such a method could look like: public void equals ( joe rr) { return ((i == rr.i) && (j == rr.j)); } Therefore, the general rule is use == and != on primitive values. Use equals for objects. If you define a new class, define an object method equals in that class. we