User Tools

Site Tools


cs257e17:proj2

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
cs257e17:proj2 [2017/03/03 17:25]
scarl created
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> 
 +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 8: Line 17:
    y = super.top();​    y = super.top();​
    ​super.pop();​    ​super.pop();​
-   z = (short) x + y;+   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>​ 
cs257e17/proj2.txt · Last modified: 2021/03/09 14:58 by scarl