This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
cs257e17:proj2 [2017/03/03 17:39] scarl |
cs257e17:proj2 [2021/03/09 14:58] (current) scarl |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | It appears Java 8 has tightened up rules on casting. To fix some of the problems: | + | It appears newer versions of Java tightened up rules on casting. To fix some issues I've seen: |
- | Prefer calling **superclass** methods to accessing the protected instance variables. For example: | + | In ''dump'', trying to output members of the ''elements'' array can cause casting errors at runtime. A simple workaround is |
+ | |||
+ | <code java> | ||
+ | Object[] elementsCopy = elements; | ||
+ | for (int i = 0; i <= topIndex; i++) { | ||
+ | System.out.println((Short) elementsCopy[i]); | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | When using inheritance, prefer calling **superclass** methods to accessing protected instance variables. For example: | ||
<code java> | <code java> | ||
short x, y, z; | short x, y, z; | ||
Line 10: | Line 19: | ||
z = (short) (x + y); // cast because type of the result is int | z = (short) (x + y); // cast because type of the result is int | ||
</code> | </code> | ||
- | |||
- | In dump, trying to output members of the ''elements'' array can cause casting errors at runtime. A simple workaround is | ||
- | |||
- | <code java> | ||
- | Object[] elementsCopy = elements; | ||
- | for (int i = 0; i <= topIndex; i++) { | ||
- | System.out.println((Short) elementsCopy[i]); | ||
- | } | ||
- | </code> |