SWE/CS 332 In Class Exercise # 23
November 15, 2017

Name(s):
Given the following variable declarations, independently consider the given 8 sequences of Java instructions inside the method GMethod(). Identify any code that results in a compiler error or warning. Identify any code that raises a runtime exception. Once a compiler error is noted, you do not need to analyze the sequence further.
 public class GClass<E> {
    private E []           elements;
    private Object[]       objects;
    private List           rawList;
    private List <Object>  objectList;
    private List <String>  stringList;

    public void GMethod ( E item ) {
        // see below
    }
 }
  1. objects = new String[1]; 
    objects[0] = "bat";     
    objects[0] = 7;        
    objects[0] = item;     
    
  2. objects = new Object[1];
    objects[0] = "bat";   
    objects[0] = 7;     
    objects[0] = item;     
    
  3. elements = new E [1];
    elements [0] = item;   
    
  4. elements = (E[]) new Object [1];
    elements [0] = item;   
    
  5. stringList = new ArrayList <String>();
    stringList.add("bat") ;
    
  6. objectList = new ArrayList <String>();
    objectList.add("bat") ;
    
  7. objectList = new ArrayList <Object>(); 
    objectList.add("bat") ;      
    objectList.add(7) ;        
    
  8. rawList = new ArrayList();
    rawList.add("bat") ;    
    rawList.add(7) ;