This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
cs257e17:proj2 [2017/03/03 17:39] scarl |
cs257e17:proj2 [2021/03/09 14:58] 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> | + | |
- | short x, y, z; | + | |
- | x = super.top(); | + | |
- | super.pop(); | + | |
- | y = super.top(); | + | |
- | super.pop(); | + | |
- | z = (short) x + y; | + | |
- | </code> | + | |
- | + | ||
- | In dump, trying to output members of the ''elements'' array can cause casting errors at runtime. A simple workaround is | + | |
<code java> | <code java> | ||
Line 19: | Line 9: | ||
} | } | ||
</code> | </code> | ||
+ | |||
+ | When using inheritance, prefer calling **superclass** methods to accessing protected instance variables. For example: | ||
+ | <code java> | ||
+ | short x, y, z; | ||
+ | x = super.top(); | ||
+ | super.pop(); | ||
+ | y = super.top(); | ||
+ | super.pop(); | ||
+ | z = (short) (x + y); // cast because type of the result is int | ||
+ | </code> |